title: Groovy map and Java map on Generics
body: <p>I am new to Groovy, and I have a question regarding to the use of map:</p>
  <p>I know I can do:</p>
  <p><code>def map = [key:&quot;value&quot;]</code></p>
  <p>But what does it mean if I do this:</p>
  <p><code>Map&lt;String, String&gt; map = [&quot;1&quot;:1, &quot;2&quot;:&quot;2&quot;]</code></p>
  <p>This code compiles, but the map is not really a String-&gt;String map:</p>
  <p><code>map.each({println it.key + &quot;:&quot; + it.value + &quot;[&quot; + it.value.class + &quot;]&quot;})</code></p>
  <p>It prints:</p>
  <blockquote>
  <p>1:1[class java.lang.Integer]</p>
  <p>2:2[class java.lang.String]</p>
  </blockquote>
  <p>Can anyone helps me to understand how come a map explicitly typed with String-&gt;String can be assigned to a map object that contains String-&gt;Integer? Thank you!</p>
  <p>=== Update ===</p>
  <p>Thanks for the link provided by @GrailsGuy, if I am using <a href="http://groovy.codehaus.org/gapi/groovy/transform/TypeChecked.html" rel="noreferrer">@TypeChecked</a> for the above code wrapped in a method, it will throw an error:</p>
  <p><code>[Static type checking] - Incompatible generic argument types. Cannot assign java.util.Map &lt;java.lang.String, java.io.Serializable&gt; to: java.util.Map &lt;String, String&gt;</code></p>
  <p>The explanation makes perfect sense now.</p>