Given I have a map like:
def myMap = [ b : [ c:"X" ] ]
And a string
def key = 'b.c'
I'd like to see my options for using the key to get to the value 'X'.
I have come up with two ways of achieving this myself, but I am not very happy with these solutions:
1) Eval.me("theMap", myMap, "theMap.$key")
2) mayMap."$key.split('\\.')[0]"."$key.split('\\.')[1]"
Anyone has a better way of doing this in Groovy?
The easiest way to merge two maps in Groovy is to use + operator. This method is straightforward - it creates a new map from the left-hand-side and right-hand-side maps.
Maps are generally used for storing key-value pairs in programming languages. You have two options to declare a map in groovy. First option is, define an empty map and put key-value pairs after. Second option is declaring map with default values.
Maps don't have an order for the elements, but we may want to sort the entries in the map. Since Groovy 1.7. 2 we can use the sort() method which uses the natural ordering of the keys to sort the entries. Or we can pass a Comparator to the sort() method to define our own sorting algorithm for the keys.
A hashmap maps a key to a value, but you are trying to map a key to two values. To do this, create a hashmap, where the key is your ID and the value is another hashmap, mapping the string "firstname" to "Jack" and the string "lastname" to "Sparrow" and so on for all <person> elements.
A convenient way is to use ConfigObject which implements Map.
def myMap = [b:[c:'X', d: 'Y'], a:[n:[m:[x:'Y']]]] as ConfigObject
def props = myMap.toProperties()
assert props['b.c'] == 'X'
assert props.'b.c' == 'X'
assert props.'a.n.m.x' == 'Y'
Pros:
IMHO its not the ConfigObject
, that does the trick, its the Properties
(from ConfigObject.toProperties()
). Look & try:
def props = new ConfigSlurper().parse("""
b {
c = 'X'
d = 'Y'
}
a {
n {
m {
x:'Y'
}
}
}""")
assert props['b.c'] == 'X'
assert props.'b.c' == 'X'
assert props.'a.n.m.x' == 'Y'
'passed'
Assertion failed:
assert props['b.c'] == 'X'
| | |
| [:] false
[b:[c:X, d:Y], a:[n:[m:[:]]], b.c:[:]]
at ConsoleScript7.run(ConsoleScript7:14)
and I really wish, the ConfigObject
could be indexed with such combined keys like above
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With