Groovy web console

subscribe to the feed Subscribe
to this
site

maps v3

Published 3 months ago by nelson lopez with tags maps lists getter setter npe
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
/* fun with Maps LePez */ 
/* Premise - this snippet is part of build script where libs are scanned to capture submod chain.  
*  While at it- I'm learning groovy and maps
*  map m's key is a module with a list of zero to many (0:M) callers */
m=[ qq1:null ,  c120:['qq1'],  c130:['qq1','c120'] ]


// Setters
m.put('i100', null)       // new key 
m[(key)].add 'added'
println m                 //print the whole map
 
// getters
println m.c120        //get value by key. Notice no quotes needed for the key 
key='c130'            //set a variable for a key 
println m[(key)][1]   //get with variable needs [( )].  Notice how you can also access list value item  by relative index  
m[(key)].each { v ->  println 'mem of ' + key +"="+ v}  // iterate a key set

println m.qq1?.size   // use the ? to avoid npe on empty values. null values have no size