issues when Iterate Map with list as value using groovy -
def map = new hashmap<string,list<string>>() def list = new arraylist<string>() def list1 = new arraylist<string>() list.add("hello1") list.add("world1") list.add("sample1") list.add("sample1") list1.add("hello2") list1.add("world2") list1.add("sample2") list1.add("sample2") map.put("abc",list) map.put("bcd",list1) def data = new arraylist<string>() for(e in map){ println "key = ${e.key} value=${e.value}" // data = "${e.value} string[]" data = "${e.value}" println "size ${data.size()} " --(b) check(data) } def check(input) { println "${input.size()}" ---(a) for(item in input){ print "$item "} }
i have pass string[] java function groovy script. trying read array list , convert string array. problem when assign {e.value}
variable data
, try size data.size()
(both step (a) , (b) ). , size 34
. counting each character not word whole list. want iterate on each word list. kindly let me know how resolve problem.
sample output
key = abc value=[hello1, world1, sample1, sample1] size 34
here groovified vesion of creating map , accessing it:
def map = [abc:['hello1', 'world1','sample1', 'sample1'], bcd:['hello2', 'world2','sample2', 'sample2']] map.collect{ println "key: ${it.key}, list: ${it.value}, , size: ${it.value.size()}" }
output:
key: abc, list: [hello1, world1, sample1, sample1], , size: 4 key: bcd, list: [hello2, world2, sample2, sample2], , size: 4
if want convert list array can it:
def list = ['hello1', 'world1','sample1', 'sample1'] assert list instanceof list def array = list string[] assert array instanceof string[] assert !(array instanceof list)
Comments
Post a Comment