Reputation: 33
Is there any way to convert numbers to strings in pure Groovy OR concatenate a number and a string to a string. It almost seems like you can.
For example:
def t = [:]
t['a' << 24] = "happy"
println t
println t.get('a24')
Line 3 will display something like [a24:happy]
but it will not be retrievable in line 4. Changing the 2nd line to t['a24'] = "happy"
displays seemingly the same print on line 3 but it actually fetches the result in the 4th line.
If I check the types on each, they both return class java.util.LinkedHashMap
So, again, is there a pure Groovy way of converting a number into a string and/or combining string + number or must it be done in Java?
Upvotes: 0
Views: 913
Reputation: 159086
Groovy implements <<
by calling leftShift()
, so 'a' << 24
actually calls String.leftShift(Object value)
, which returns a StringBuffer
, not a String
.
That means that your map key is a StringBuffer
, so calling get('a24')
, where you pass in a String
, will not find anything.
You can see that by printing the type:
t.each { k, v -> println k.getClass() } // prints: class java.lang.StringBuffer
Upvotes: 2