Note that there are some explanatory texts on larger screens.

plurals
  1. POJava HashMap nested generics with wildcards
    primarykey
    data
    text
    <p>I'm trying to make a hashmap of hashmap values containing hashsets of different subclasses of a custom class, like so:</p> <pre><code>HashMap&lt;String, Hashmap&lt;String, HashSet&lt;? extends AttackCard&gt;&gt;&gt; superMap </code></pre> <p><code>AttackCard</code> has subclasses such as: <code>Mage</code>, <code>Assassin</code>, <code>Fighter</code>. Each HashMap in the superMap will only ever have HashSets containing a single subclass of <code>AttackCard</code>.</p> <p>When I try putting a</p> <pre><code>HashMap&lt;String, HashSet&lt;Assassin&gt;&gt; </code></pre> <p>into superMap, I get a compiler error: <a href="https://i.stack.imgur.com/jnp5O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jnp5O.png" alt="comiler error"></a></p> <p>below is the code where the error occurs:</p> <pre><code>public class CardPool { private HashMap&lt;String, HashMap&lt;String, HashSet&lt;? extends AttackCard&gt;&gt;&gt; attackPool = new HashMap&lt;&gt;(); private ArrayList&lt;AuxiliaryCard&gt; auxiliaryPool; public CardPool() { (line 24)this.attackPool.put("assassins", new AssassinPool().get()); /* this.attackPool.put("fighters", new Fighter().getPool()); this.attackPool.put("mages", new Mage().getPool()); this.attackPool.put("marksmen", new Marksman().getPool()); this.attackPool.put("supports", new Support().getPool()); this.attackPool.put("tanks", new Tank().getPool()); */ this.auxiliaryPool = new ArrayList&lt;&gt;(new AuxiliaryCard().getPool()); } </code></pre> <p>And here is a snippet of the AssassinPool get-method:</p> <pre><code>private HashMap&lt;String, HashSet&lt;Assassin&gt;&gt; pool = new HashMap&lt;&gt;(); public HashMap&lt;String, HashSet&lt;Assassin&gt;&gt; get() { return pool; } </code></pre> <p>I'd like to comment that I could easily solve my problem and have a wonderfully working program by making all the AttackCardPools, such as AssassinPool, return and contain HashSets of AttackCard instead of their respective subclass. I'm trying to understand this compilation error, however :)</p> <pre><code>compilation error at line 24: error: no suitable method found for `put(String, HashMap&lt;String,HashSet&lt;Assassin&gt;&gt;&gt;` this.attackPool.put("assassins", new AssassinPool(). get()); method HashMap.putp.(String, HashMap&lt;String,HashSet&lt;? extends AttackCard&gt;&gt;&gt;` is not applicable (actual argument `HashMap&lt;String, HashSet&lt;Assassin&gt;&gt;` cannot be converted to `HashMap&lt;String, HashSet&lt;? extends AttackCard&gt;&gt;` by method invocation conversion) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload