title: Merging multiple maps in Java 8
body: <p>Java 8 here. I have the following classes:</p>
  
  <pre><code>public interface Animal {
    ...
  }
  
  public class Dog implements Animal {
    ...
  }
  
  public class Cat implements Animal {
    ...
  }
  
  public class Elephant implements Animal {
    ...
  }
  </code></pre>
  
  <p>I have to implement the following method:</p>
  
  <pre><code>void doSomething(Map&lt;String,Dog&gt; dogs, Map&lt;String,Cat&gt; cats, Map&lt;String,Elephant&gt; elephants) {
    // TODO:
    // * Merge all dogs, cats &amp; elephants together into the same Map&lt;String,Animal&gt;,
    //     but...
    // * Do so generically (without having to create, say, a HashMap instance, etc.)
  }
  </code></pre>
  
  <p>In my <code>doSomething(...)</code> method, I need to merge all the map arguments into the same <code>Map&lt;String,Animal&gt;</code> map, <strong>but</strong> I'd really prefer to do so without <strong>my code</strong> having to instantiate a specific map implementation (such as <code>HashMap</code>, etc.).</p>
  
  <p>Meaning, I <em>know</em> I could do this:</p>
  
  <pre><code>void doSomething(Map&lt;String,Dog&gt; dogs, Map&lt;String,Cat&gt; cats, Map&lt;String,Elephant&gt; elephants) {
    Map&lt;String,Animal&gt; animals = new HashMap&lt;&gt;();
    for(String dog : dogs.keySet()) {
      animals.put(dog, dogs.get(dog));
    }
    for(String cat : cats.keySet()) {
      animals.put(cat, cats.get(cat));
    }
    for(String elephant : elephants.keySet()) {
      animals.put(elephant, elephants.get(elephant));
    }
  
    // Now animals has all the argument maps merged into it, but is specifically
    // a HashMap...
  }
  </code></pre>
  
  <p>I'm even fine using some utility if it exists, like maybe a <code>Collections.merge(dogs, cats, elephants)</code>, etc. Any ideas?</p>