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 6619299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:56:55+00:00 2026-05-25T20:56:55+00:00

I was writing code and I noticed some odd behavior in Groovy when I

  • 0

I was writing code and I noticed some odd behavior in Groovy when I am dealing with XML and Maps. I thought about it and can’t figure out why is it happening and should it that way.

I wrote sample code with 3 examples. Crucial difference between map1 & map3 is only on the following part:

Map1:

map1 << ["${it.name()}":it.value()]

Map3:

map3["${it.name()}"]=it.value()

Here is full code, you can copy-paste it into Groovy console:

def xml = '<xml><head>headHere</head><body>bodyHere</body></xml>'


Map map1 = [:]

def node = new XmlParser().parseText(xml) 

node.each {
      map1 << ["${it.name()}": it.value()]
} 

println map1
println map1["head"]

println ">>>>>>>>>>>>>>>>>>>>>>"



Map map2 = [:]

map2 << ["head":"headHere"]
map2 << ["body":"bodyHere"]

println map2
println map2["head"]

println "<<<<<<<<<<<<<<<<<<<<<<"



def xml2 = '<xml><head>headHere</head><body>bodyHere</body></xml>'    

Map map3 = [:]

def node2 = new XmlParser().parseText(xml2) 

node2.each {
      map3["${it.name()}"]=it.value()
} 

println map3
println map3["head"]

The result that I am getting is following:

[head:[headHere], body:[bodyHere]]
null

[head:headHere, body:bodyHere]
headHere

[head:[headHere], body:[bodyHere]]
[headHere]

Even thou map1 and map3 look the same, the result of map[“head”] is totally different, first gives null and second gives the actual result. I don’t understand why is it happening. I spent some time on it and still don’t get it. I used .getProperty() to get info on a class, but it looks the same and feels the same on both maps and object inside. I tried couple more things and nothing gives me any idea on what is happening. I even tried different OS (Win XP, Mac OS) and still nothing.

I don’t have any ideas anymore, please can one some explain odd behavior, why is it happening and what is the difference between map << [key:object] and map[key] = object?

Thank you.

  • 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-05-25T20:56:56+00:00Added an answer on May 25, 2026 at 8:56 pm

    One thing that might help is, don’t use GStrings for your keys. Groovy supports using objects directly as keys by wrapping them in parentheses.

    From the manual:

    Map keys are strings by default: [a:1] is equivalent to [“a”:1]. But if you really want a variable to become the key, you have to wrap it between parentheses: [(a):1].

    Fully working example:

    def xml = '<xml><head>headHere</head><body>bodyHere</body></xml>'
    
    Map map1 = [:]
    def node = new XmlParser().parseText(xml)
    node.each {
        map1 << [ (it.name()): it.value() ]
    }
    
    println map1
    println map1["head"]
    println ">>>>>>>>>>>>>>>>>>>>>>"
    
    Map map2 = [:]
    
    map2 << ["head":"headHere"]
    map2 << ["body":"bodyHere"]
    
    println map2
    println map2["head"]
    
    println "<<<<<<<<<<<<<<<<<<<<<<"
    
    def xml2 = '<xml><head>headHere</head><body>bodyHere</body></xml>'
    
    Map map3 = [:]
    
    def node2 = new XmlParser().parseText(xml2)
    
    node2.each {
        map3[it.name()] = it.value()
    }
    
    println map3
    println map3["head"]
    

    The output is:

    [head:[headHere], body:[bodyHere]]
    [headHere]
    >>>>>>>>>>>>>>>>>>>>>>
    [head:headHere, body:bodyHere]
    headHere
    <<<<<<<<<<<<<<<<<<<<<<
    [head:[headHere], body:[bodyHere]]
    [headHere]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking into writing simple graphics code in Android and I've noticed some synchronized()
I was just writing some quick code and noticed this complier error Using the
While writing Scala RemoteActor code, I noticed some pitfalls: RemoteActor.classLoader = getClass().getClassLoader() has to
I've noticed a common pattern in some code I'm writing so I decided to
I was just writing some simple code and I noticed that using document.writeln doesn't
I'm writing some low level code and I noticed some unnecessary level of indirection
I was writing some code, and I notice a pattern in the exception handling
In writing some threaded code, I've been using the ReaderWriterLockSlim class to handle synchronized
I have been writing some C# code for a training exercise, in which I
I was writing some practice programs for my java certification this morning, and noticed

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.