Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>That map is OK; if you really need to add a tiny bit of constraint, just try:</p> <pre><code>Map&lt;Class&lt;? extends EntityProxy&gt;, HashMap&lt;Long, EntityProxy&gt;&gt; indexMap; </code></pre> <p>This would make sure the key class can only be an EntityProxy.class or subclass.</p> <p>You can think of the question mark as some "anything", but anonymous. So <code>&lt;?&gt;</code> means <em>really anything</em> -- any <code>Object</code>, <code>&lt;? extends EntityProxy&gt;</code> means anything that fulfils this condition (passes the "<code>instanceof EntityProxy</code>" test).</p> <p>The type safety here is less than desired, as you can still put anything as key and anything in that map. I mean, you can legally put this in the map:</p> <pre><code>indexMap.put(EntityProxy1.class, new HashMap&lt;Long, EntityProxy2&gt;()); </code></pre> <p>(assuming <code>EntityProxy1</code> and <code>EntityProxy2</code> are both subclasses of <code>EntityProxy</code>) since there's no correlation between the key and the value. To enforce that, the <code>put()</code> method of the map would need to be declared like this:</p> <pre><code>&lt;T extends EntityProxy&gt; put(Class&lt;T&gt; key, HashMap&lt;Long, T&gt; value); </code></pre> <p><code>T</code> is pretty much like <code>?</code> but the main difference is that it provides you with a name that you can refer to it in that context. So, if I said that <code>?</code> stands for "anything", I would say <code>T</code> stands for "something", as you can refer to that something once you declare it.</p> <p>But you would need a custom data structure for this, as <code>java.util.Map</code> does not provide this kind of constraint. If you're using it as shown in your code sample, I don't think you really need these enforcements.</p>
 

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