I have a groovy list as below
def certs = ['0xc1','0xc1','0xc1','0xc1','0xc2','0xc2','0xc3','0xc4','0xc4','0xc5','0xc5','0xc5','0xc5']
Am trying to find the occurance of each element and group by its count. I've tried
certs.groupBy { it }.findAll { it.value.size() }
but am getting the below output
[0xc1:[0xc1, 0xc1, 0xc1, 0xc1], 0xc2:[0xc2, 0xc2], 0xc3:[0xc3], 0xc4:[0xc4, 0xc4], 0xc5:[0xc5, 0xc5, 0xc5, 0xc5]]
Instead am expecting below
[0xc1:4, 0xc2:2, 0xc3:1, 0xc4:2, 0xc5:4]
can someone help me with this? Also I wanna find maximum occurring element in the list in my case its 0xc1
and 0xc5
UPDATE:
def myMap = certs.inject([:]) { m, x -> if (!m[x]) m[x] = 0; m[x] += 1; m }
def maxValue = myMap.values().max{it}
def myKeys = []
myMap.findAll{ it.value == maxValue }.each{myKeys << it?.key}
println myKeys // result = [0xc1:4, 0xc5:4]
//println myMap.sort { a, b -> b.value <=> a.value }