Apex:map.Put()byVal而不是byRef?

时间:2015-02-03 16:06:14

标签: java dictionary salesforce apex

我想知道是否有办法通过值而不是通过引用将.put()元素放入地图中。

我有一张地图>和一个填充它的循环,如下所示:

    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;
            }                
        }
    }

问题是当我清除临时列表时。然后我得到一个具有键的映射,但是空列表值。是否可以在地图中按值(。创建一个副本).put(),以便它不受对temp.clear()的调用的影响?

谢谢大家,

扎克

2 个答案:

答案 0 :(得分:2)

你可以像这样修改你的代码。在这段代码中,我为每个地图插入创建一个新实例。

地图&GT; result = new Map&gt;();     列出临时文字;

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;
        }                
    }
}

答案 1 :(得分:1)

我认为你应该这样做

            if(j.id == i.id){
               List<string> temp = new list<string>();
               temp.add(j.Contact__r.Owner.Id);
               temp.add(j.Contact__r.Id);
               result.put(i.Deal__r.id, temp);