groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <blackd...@gmx.org>
Subject Re: Auto-generating a map key
Date Thu, 23 Jul 2015 05:49:38 GMT
Am 23.07.2015 00:39, schrieb Lowery, Jeff:
> Trying to find the simplest way to auto generate a map key; the best
> I've come up with is:
>
> def c=0
>
> def map = {
>     "_KEY$c++" : "foo", "_KEY$c++": "bar", ... }

First of all: You shall not use GString as map key.
a) GString is a mutable, using mutables as map keys can lead to very 
interesting behaviour of the map
b) GString is not having the same hashcode as String even if the 
toString() representation is the same.

Factor (a) is probably not a problem here, but (b) can easily cause a 
problem.

> Is there better?

you are not really giving enough information. For example, why not 
simply use a list here? The key seems not to play an important role, 
thus I doubt that searching is the problem you try to solve with the 
map. But then it is questionable if a map is even the right 
datastructure for your problem.


bye blackdrag

-- 
Jochen "blackdrag" Theodorou
blog: http://blackdragsview.blogspot.com/


Mime
View raw message