-1

我的以下代码

def traineeDetails = session.traineeDetailsForAuto

在印刷上给出:

traineeDetails = [name:[Hus, Vin], email:[hus@gmail.com, vin@gmail.com], phone:[9908877654, 9987655432], jobTitle:[SE, ST]]

def count = traineeDetails.name.size()  gives correct value =2

但是当映射键包含一个值时

 def traineeDetails = session.traineeDetailsForAuto

在印刷上给出:

 traineeDetails = [name:Hus, email:hus@gmail.com, phone:9987766543, jobTitle:SE]

 def count= traineeDetails.name.size() gives wrong answer 3 which is the total number of character in name

但在这里我需要获取键名持有的值的总数..

怎么做?

4

1 回答 1

2

如果要在地图中混合类型,则需要检查类型:

def count = traineeDetails.name.with { it instanceof Collection ? it.size() : 1 }

使用您的示例,它可以正常工作:

traineeDetails = [name:['Hus', 'Vin'], email:['hus@gmail.com', 'vin@gmail.com'], phone:['9908877654', '9987655432'], jobTitle:['SE', 'ST']]
count = traineeDetails.name.with { it instanceof Collection ? it.size() : 1 }

assert count == 2

traineeDetails = [name:'Hus', email:'hus@gmail.com', phone:'9987766543', jobTitle:'SE']
count = traineeDetails.name.with { it instanceof Collection ? it.size() : 1 }

assert count == 1

你能提供实际的非工作示例吗?

于 2013-09-11T07:41:06.527 回答