我的以下代码
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
但是在这里我需要获得密钥名称所包含的值的总计数。
怎么做?
答案 0 :(得分: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
您能提供实际的非工作示例吗?