如何将Java的hashMap内容全部放在另一个,但不能替换现有的键和值?

rap*_*ura 79 java hashmap map

我需要将所有键和值从一个HashMap复制到另一个B,但不要替换现有的键和值.

什么是最好的方法呢?

我正在考虑迭代keySet和checkig是否存在,我愿意

Map temp = new HashMap(); // generic later
temp.putAll(Amap);
A.clear();
A.putAll(Bmap);
A.putAll(temp);
Run Code Online (Sandbox Code Playgroud)

eri*_*son 103

看起来你愿意创建一个临时的Map,所以我会这样做:

Map tmp = new HashMap(patch);
tmp.keySet().removeAll(target.keySet());
target.putAll(tmp);
Run Code Online (Sandbox Code Playgroud)

patch是您要添加到target地图的地图.

感谢Louis Wasserman,这是一个利用Java 8中新方法的版本:

patch.forEach(target::putIfAbsent);
Run Code Online (Sandbox Code Playgroud)

  • @erickson是一个更紧凑的Java 8版本:`patch.forEach(target :: putIfAbsent)`. (14认同)

Tom*_*sky 15

使用GuavaMaps类的实用程序方法来计算2个地图的差异,您可以在一行中完成它,使用方法签名可以更清楚地了解您要完成的任务:

public static void main(final String[] args) {
    // Create some maps
    final Map<Integer, String> map1 = new HashMap<Integer, String>();
    map1.put(1, "Hello");
    map1.put(2, "There");
    final Map<Integer, String> map2 = new HashMap<Integer, String>();
    map2.put(2, "There");
    map2.put(3, "is");
    map2.put(4, "a");
    map2.put(5, "bird");

    // Add everything in map1 not in map2 to map2
    map2.putAll(Maps.difference(map1, map2).entriesOnlyOnLeft());
}
Run Code Online (Sandbox Code Playgroud)


Rev*_*nzo 8

只需迭代并添加:

for(Map.Entry e : a.entrySet())
  if(!b.containsKey(e.getKey())
    b.put(e.getKey(), e.getValue());
Run Code Online (Sandbox Code Playgroud)

编辑添加:

如果您可以对a进行更改,您还可以执行以下操作:

a.putAll(b)
Run Code Online (Sandbox Code Playgroud)

一个将完全符合你的需要.(所有条目b和所有条目a都不在b)


Ana*_*iuk 5

如果你在@ erickson的解决方案中更改地图顺序,你可以只用一行:

mapWithNotSoImportantValues.putAll( mapWithImportantValues );
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您将mapWithNotSoImportantValues中的值替换为mapWithImportantValues中具有相同键的值.


Nik*_*las 5

Java 8解决方案使用Map#merge

开始,您可以使用Map#merge(K key, V value, BiFunction remappingFunction)它将值合并到MapusingremappingFunction中,以防密钥已经在Map您想要放入该对的 中找到。

// using lambda
newMap.forEach((key, value) -> map.merge(key, value, (oldValue, newValue) -> oldValue));
Run Code Online (Sandbox Code Playgroud)
// using for-loop
for (Map.Entry<Integer, String> entry: newMap.entrySet()) {
    map.merge(entry.getKey(), entry.getValue(), (oldValue, newValue) -> oldValue);
}
Run Code Online (Sandbox Code Playgroud)

该代码迭代newMap条目(key和),并且每个条目都通过该方法value合并到其中。如果出现重复的键,则会触发 ,在这种情况下,它表示将使用前一个(原始)值而不是重写。mapmergeremappingFunctionoldValue

使用此解决方案,您不需要临时Map.


让我们举一个示例,将newMap条目合并到map并保留原始值,以防出现重复的 antry。

Map<Integer, String> newMap = new HashMap<>();
newMap.put(2, "EVIL VALUE");                         // this will NOT be merged into
newMap.put(4, "four");                               // this WILL be merged into
newMap.put(5, "five");                               // this WILL be merged into

Map<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");

newMap.forEach((k, v) -> map.merge(k, v, (oldValue, newValue) -> oldValue));

map.forEach((k, v) -> System.out.println(k + " " + v));
Run Code Online (Sandbox Code Playgroud)
1 one
2 two
3 three
4 four
5 five
Run Code Online (Sandbox Code Playgroud)