我想知道是否有办法通过值而不是引用将元素 .put() 到映射中。
我有一个 map >和一个填充它的循环,如下所示:
Map<string,List<string>> result = new Map<string,List<string>>();
List<string> temp = new list<string>();
for(CustomObj__c i:customList){
for(CustomObj__c j:customList2){
if(j.id == i.id){
temp.add(j.Contact__r.Owner.Id);
temp.add(j.Contact__r.Id);
result.put(i.Deal__r.id, temp);
temp.clear();
break;
}
}
}
问题是当我清除临时列表时。然后我最终得到一个包含键但列表值为空的映射。是否可以在 map 中按值 .put() 列表(创建副本),使其不受调用 temp.clear() 的影响?
谢谢大家,
扎克
最佳答案
您可以像这样修改您的代码。在此代码中,我为每个 map 插入创建一个新实例。
map > 结果 = 新 map >(); 列出温度;
for(CustomObj__c i:customList){
for(CustomObj__c j:customList2){
if(j.id == i.id){
temp = new list<string>();
temp.add(j.Contact__r.Owner.Id);
temp.add(j.Contact__r.Id);
result.put(i.Deal__r.id, temp);
break;
}
}
}
关于java - Apex:map.Put() byVal 而不是 byRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28303465/