title: Java: Using a hashmap, retrieving all values and calling methods
body: <p>I have a need to store a list of dynamically created objects in a way where they can all be retrieved and their methods called on demand.</p>
  
  <p>As far as I can see for the list and creation, a HashMap fits my needs but i'm a bit puzzled on recalling the objects and calling their methods using the HashMap.</p>
  
  <p>Just as a reference, let me give you a little code:</p>
  
  <p>Here is the HashMap:</p>
  
  <pre><code>Map&lt;String, Object&gt; unitMap = new HashMap&lt;String, Object&gt;();
  
  // here is how I put an object in the Map notice i'm passing coordinates to the constructor:
  unitMap.put("1", new Worker(240, 240));
  unitMap.put("2", new Worker(240, 240));
  </code></pre>
  
  <p>Now I need to create a method that retrieves every object in the hashmap and call a method from each object. is this possible or can the created objects only be referenced directly. If so, is there another way to call a method of all existing instances of a class dynamically (in other words, on user input)?</p>