简体   繁体   English

如何在顶点连接两个地图

[英]How to concatenate two maps in apex

I have two maps, different sObject fields are being stored, one for each sObject.我有两张地图,存储了不同的 sObject 字段,每个 sObject 一个。 I want to add the fields from map1 to the beginning of map2 or vise-versa.我想将 map1 中的字段添加到 map2 的开头,反之亦然。

Is it possible to add the contents of one map to another without merging similar fields or keys?是否可以在不合并相似字段或键的情况下将一个 map 的内容添加到另一个? Can the values in a map that is an array be placed in such a way that they come after the values of the first map?作为数组的 map 中的值是否可以放置在第一个 map 的值之后?

You can use the putAll(fromMap) method: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm您可以使用 putAll(fromMap) 方法: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm

Map<String, String> map1 = new Map<String, String>();
map1.put('Red','FF0000');
Map<String, String> map2 = new Map<String, String>();
map2.put('Blue','0000FF');
// Add map1 entries to map2
map2.putAll(map1);
System.Debug(map2);
//map2 = {Blue=0000FF, Red=FF0000}

If you want to go the other way: map1.putAll(map2);如果你想用另一种方式 go: map1.putAll(map2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM