Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Groovy
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Groovy
map of maps in groovy
Grace Green
Ranch Hand
Posts: 79
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I would like to create a map of maps in groovy.
For each entry in idToStates, I would have
[id : [state:timestamp]]
The problem with the code below is that the [state:timestamp] is not adding new state-to-timestamp pairs, but merely replacing the existing one.
I do not seem to be able to find a solution, I might not be using the correct groovy syntax since I am new to it. Thank you in advance for your help.
def idToStates = [:] (...) def stateToTimestamp = idToStates.get(id); if (stateToTimestamp == null){ stateToTimestamp = [:] //println("Creating new map ------ "); } stateToTimestamp << [(visitedState) : timestamp]; idToStates << [(sessionId) : stateToTimestamp];
Knute Snortum
Sheriff
Posts: 7125
184
I like...
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Well, I haven't seen the "<<" operator except for moving bits, but you can use the
.put()
method:
def timestamp = new Date(1) def visitedState = 1 def stateToTimestamp = [(visitedState) : timestamp] def sessionId = 23874 def idToStates = [(sessionId) : stateToTimestamp] stateToTimestamp = [(2) : new Date(2)] idToStates.put( 8874, stateToTimestamp ) idToStates.put( 33423, [(3) : new Date(3)]) idToStates.each() { key, value -> println "${key} == ${value}" }
All things are lawful, but not all things are profitable.
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Groovy Perforamance: A Little Disapointed
How to generate XML from MAP
Example of using Groovy Closure for a different programming style
Passing objects across controllers in Grails
Can anyone explain the Groovy 'expando' class in simple terms?
More...