title: Why Map does not work for GString in Groovy?
body: <p>With the following snippet I cannot retrieve <code>gString</code> from a map: </p>
  
  <pre><code>def contents = "contents"
  def gString = "$contents"
  
  def map = [(gString): true]
  
  assert map.size() == 1 // Passes
  assert gString.hashCode() == map.keySet().first().hashCode() // Passes, same hash code
  assert map[gString] // Fails
  </code></pre>
  
  <p><strong>How on earth is that possible?</strong></p>
  
  <p>Assertion message clearly shows that there's something seriously wrong with Groovy:</p>
  
  <pre><code>assert map[gString] // Fails
         |  ||
         |  |contents
         |  null
         [contents:true]
  </code></pre>
  
  <p>It's not the same question as <a href="https://stackoverflow.com/questions/9705492/why-groovy-does-not-see-some-values-in-dictionary">Why groovy does not see some values in dictionary?</a>
  First answer there suggests:</p>
  
  <blockquote>
    <p>You're adding GString instances as keys in your map, then searching for them using String instances.</p>
  </blockquote>
  
  <p>In this question I clearly add <code>GString</code> and try to retrieve <code>GString</code>.</p>
  
  <p>Also neither <a href="https://stackoverflow.com/questions/31713359/why-are-there-different-behaviors-for-the-ways-of-addressing-gstring-keys-in-map">Why are there different behaviors for the ways of addressing GString keys in maps?</a> nor <a href="https://stackoverflow.com/questions/9682206/groovy-different-results-on-using-equals-and-on-a-gstringimpl">Groovy different results on using equals() and == on a GStringImpl</a> have an answer for me. I do not mutate anything and I do not mix <code>String</code> with <code>GString</code>.</p>