You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by Nhan Vo <th...@yahoo.com> on 2022/05/13 06:34:41 UTC

Surprising behavior when having Integer & Long in Set & Map

 Hi,

I just had a use case in Grails where I converted a JSONArray into a Set of Longs. In my UT I assert the resulting set is equal to a set of Longs: 
resultingSet == Set.of(1L)
The assertion passes, although resultSet in fact contains Integers. Apparently, this is expected: https://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/runtime/DefaultGroovyMethods.html#equals(java.util.Set,java.util.Set)
Then there's another step after that in which that set is turned into a key set of a Map, but when you do lookups, an Integer key is not considered equal to a Long key. So when I do map[1L] it gives null.
Was it indeed an intention to have numbers equal in set comparisons but not in map lookup via keys?
Thanks!
Nhan  

Re: Surprising behavior when having Integer & Long in Set & Map

Posted by Jochen Theodorou <bl...@gmx.org>.
On 13.05.22 08:34, Nhan Vo wrote:
[...]
> Was it indeed an intention to have numbers equal in set comparisons but
> not in map lookup via keys?

yes. Same for String and GString.


bye Jochen