Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your declaration is invalid.</p> <h1>Using Generics (Parameterized Types)</h1> <p>Using Java 5 to 6, you should write:</p> <pre><code>Map&lt;String, List&lt;Map&lt;String, ParseNode&gt;&gt;&gt; m = new HashMap&lt;String, List&lt;Map&lt;String, ParseNode&gt;&gt;&gt;(); </code></pre> <p>Starting with Java 7, you can simplify this with:</p> <pre><code>Map&lt;String, List&lt;Map&lt;String, ParseNode&gt;&gt;&gt; m = new HashMap&lt;&gt;(); </code></pre> <p>Your syntax means something different and uses a <strong>raw type</strong>:</p> <pre><code>Map&lt;String, List&lt;Map&lt;String, ParseNode&gt;&gt;&gt; m = new HashMap(); </code></pre> <p><strong>Update:</strong></p> <p>To answer your question, this has implications (as mentioned by the <a href="http://docs.oracle.com/javase/tutorial/" rel="nofollow noreferrer">Java Tutorial</a>):</p> <blockquote> <p>Note that to take advantage of automatic type inference during generic class instantiation, you must specify the diamond. [In your case] the compiler generates an unchecked conversion warning because the HashMap() constructor refers to the HashMap raw type, not the [parameterized Map] type.</p> </blockquote> <p>Basically meaning that the parameterization of your type here doesn't have much meaning. The code following this declaration will be compiled with the assumptation that it contains values with the types as declared. However at runtime, you declared a type of a raw type, and you could very well have assigned a map containing different values to this entry.</p> <p>What you wrote would be the equivalent of writing something like:</p> <pre><code>Map rawMap = new HashMap(); rawMap.add("string", "not list!"); Map&lt;String, List&lt;Map&lt;String, ParseNode&gt;&gt;&gt; m = rawMap; // uh oh!! </code></pre> <p>Which would compile fine, and blow up in your face at runtime when you try to access one of <code>m</code>'s values as a <code>List&lt;Map&lt;String, ParseNode&gt;&gt;</code>.</p> <h1>Reasons</h1> <p>The reason for this is that Generics where introduced while conserving complete backwards compatibility at the source level, hence some of their limitation, like:</p> <ul> <li>the impossibility to have a short-hand form without at least some indicator for generics support (here, the so-called <strong>diamond operator</strong> <code>&lt;&gt;</code>),</li> <li>the impossibility to inspect generic-types at runtime, because they had to be implemented with <strong><a href="http://docs.oracle.com/javase/tutorial/java/generics/erasure.html" rel="nofollow noreferrer">Type Erasure</a></strong>.</li> </ul> <h2>Further Reading</h2> <ul> <li>From <a href="http://docs.oracle.com/javase/tutorial/" rel="nofollow noreferrer">The Java Tutorial</a>: <ul> <li>section on <a href="http://docs.oracle.com/javase/tutorial/java/generics/gentypes.html" rel="nofollow noreferrer">Generic Types</a></li> <li>section on <a href="http://docs.oracle.com/javase/tutorial/java/generics/gentypeinference.html#type-inference-instantiation" rel="nofollow noreferrer">Type Inference and Instantiation of Generic Classes</a></li> </ul></li> <li>From the Java <a href="http://docs.oracle.com/javase/specs/" rel="nofollow noreferrer">Language Specifications</a> (JLS): <ul> <li><a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/j3TOC.html" rel="nofollow noreferrer">Java SE 5's JLS</a> section on <a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html" rel="nofollow noreferrer">Types, Values and Variables</a></li> <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/index.html" rel="nofollow noreferrer">Java SE 7's JLS</a> section on <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html" rel="nofollow noreferrer">Types, Values and Variables</a></li> </ul></li> <li>From good StackOverflow questions: <ul> <li><a href="https://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7">What is the point of the diamond operator in Java 7?</a></li> <li><a href="https://stackoverflow.com/questions/1206028/java-raw-type-and-generics-interaction">Java Raw Type and generics interaction</a></li> </ul></li> <li>Others: <ul> <li>IBM Developer Series: <a href="http://www.ibm.com/developerworks/java/library/j-jtp01255/index.html" rel="nofollow noreferrer">Java Theory and Practice: Generics Gotchas</a></li> </ul></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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