Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7910219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:47:09+00:00 2026-06-03T12:47:09+00:00

I’m running into some confusing behaviour from LinkedHashMap in grails 2.0.3. Running the following

  • 0

I’m running into some confusing behaviour from LinkedHashMap in
grails 2.0.3. Running the following script in grails console:

def m = ["smart-1":[stuff:'asdf']]
println m.getClass()

def p = [id:1]
println m."smart-$p.id"
println m["smart-$p.id"]
println m.get("smart-$p.id")

println m.'smart-1'
println m['smart-1']
println m.get('smart-1')

gives the output:

class java.util.LinkedHashMap
[stuff:asdf]
[stuff:asdf]
null
[stuff:asdf]
[stuff:asdf]
[stuff:asdf]

In an integration test, I’m seeing the opposite behaviour – I can only
get the content of the HashMap using m.get(GStringImpl) (as opposed
m.get(String)).

Is this behaviour expected or known?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:47:16+00:00Added an answer on June 3, 2026 at 12:47 pm

    First: don’t use GStrings in your hashmap keys. Ever. You will almost always have an issue retrieving the item, because a GString is not a String (red box on that page), and does not have the same hash value. Instead, use one of these options:

    def key = 'key'
    ['key': value]
    [(key): value]
    [("some $key".toString()): value]
    

    This ensures that you will always get the result when using a String. (So, for lookups, always use a String, too.)

    I’m not 100% sure why you are seeing the odd behavior, but I have a solid guess. The get() method is a Java method, while the array-style (and probably property-style) lookup is implemented using getAt(), which is a Groovy (GDK) method. My guess is that the Groovy method is aware of GStrings, and silently handles the conversion to make sure you don’t get tripped up.

    The simplest solution is to always use getAt(), not get:

    def m = ['smart-1':[stuff:'asdf']]
    println m.getClass()
    
    def p = [id:1]
    println m."smart-$p.id"
    println m["smart-$p.id"]
    println m.getAt("smart-$p.id")
    
    println m.'smart-1'
    println m['smart-1']
    println m.getAt('smart-1')
    

    Which works fine.

    The better solution is to ensure that you are using Strings when looking up values, like so:

    println m.get("smart-$p.id".toString())
    

    Which also works. I like this method better, because when calling the method directly, it’s clearer that your key is a String. I’d still use the normal GString when using the array-style or property-style accessors, because that’s standard Groovy syntax.


    In an integration test, I’m seeing the opposite behaviour – I can only get the content of the HashMap using m.get(GStringImpl) (as opposed m.get(String)).

    This is most likely because your key in your hashmap is staying a GString.

    If a GString does not have any variables, the Groovy compiler silently converts it to a String literal (better performance), which is why the above example is actually using a String as the key, but the lookup is using a GString.

    e.g.

    "Hello $name" -> GString('Hello $name')
    "Hello Bob"   -> 'Hello Bob'
    

    A final thought: As long as you are in groovy, don’t use get(), since Groovy provides the much cleaner [] and property syntaxes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.