groovy - lazy instantiation of the map value -
is there way instantiate value of map lazy?
for example
class maptest { @lazy(soft = true) hashmap<string, list<string>> map } doing can use call , null without recieving nullpointerexception
new maptest().map.key1 however attempt call
map.key1.remove(1) will lead nullpointerexception due value being null. (it fine if threw indexoutofbounds exception)
is there way instantiate list value of map?
try map.withdefault :
def map = [:].withdefault { [] } assert map.key1.isempty() some explanation :
- [:] groovy way instantiate empty hash map
withdefaultgroovy method on map wich take closure. closure call every time key requested initialize value if doesn't exist. closure take 1 parameter (the key) , should value- [] groovy way create empty list - { [] } closure wich return empty list every key
see others examples here
Comments
Post a Comment