Reputation: 3915
I have a map,
def map= [name:[Vin], email:[[email protected]], phone:[9988888888], jobTitle:[SE]]
i want get the total number of values that a key holds
for ex,
key name
can have many values like [name:[Vin,Hus,Rock]
how to do it programatically?
def count = map.name.size() //gives wrong answer
Upvotes: 0
Views: 7810
Reputation: 50245
def map= [name :['Vin', 'abc', 'xyz'],
email:['[email protected]'],
phone:[9988888888],
jobTitle:['SE']]
//Spread operator to get size of each value
assert map.values()*.size == [3, 1, 1, 1]
//Implicit spread
assert map.values().size == [3, 1, 1, 1]
//use size() to get the size of the values collection
assert map.values().size() == 4
//Values
assert map.values() as List == [['Vin', 'abc', 'xyz'],
['[email protected]'], [9988888888], ['SE']]
Upvotes: 1
Reputation: 1344
You can use the following code to get a list of size for all key.
def map= [name:['Vin',''], email:['[email protected]'], phone:['9988888888'], jobTitle:['SE']]
map.collect{it.value.size()}
Output:
[2, 1, 1, 1]
I think map.name.size() should work fine too in groovy.
Upvotes: 2