简体   繁体   English

将密钥从一个 map 复制到另一个

[英]Copying keys from one map to another

I have two maps.我有两张地图。 One with default values of 0 and one with set values.一种具有默认值 0,另一种具有设定值。 For example例如

Map<String,Integer> defaultTaskScores = new HashMap<String, Integer>(){{
   put("Phalanxing", 0);
   put("Shieldwalling",0);
   put("Tercioing",0);
   put("Wedging",0);
}};

Map<String,Integer> taskScores = new HashMap<String, Integer>(){{
   put("Phalanxing", 90);
   put("Shieldwalling",56);
   put("Wedging",24);
}};

I want to put into taskScores a pair of key-value from defaultTaskScores which key's isn't in taskScores.我想将 defaultTaskScores 中的一对键值放入 taskScores 中,其中键不在 taskScores 中。 For this example it's putting Tercioing with value of 0.对于此示例,它将 Tercioing 的值设置为 0。

taskScores maps are in the list taskScores 地图在列表中

List<CourseResult> courseResultList;

public class CourseResult {
  private final Person person;
  private final Map<String, Integer> taskResults;

  public CourseResult(final Person person, final Map<String, Integer> taskResults) {
      this.person = person;
      this.taskResults = taskResults;
  }
}

You could iterate over defaultTaskScores and use putIfAbsent to add the missing keys to taskScores :您可以遍历defaultTaskScores并使用putIfAbsent将缺少的键添加到taskScores

defaultTaskScores.keySet().forEach(k -> taskScores.putIfAbsent(k, 0));

EDIT:编辑:
An alternate approach could be to apply the default value when retrieving a score from the map.另一种方法是在从 map 检索分数时应用默认值。 Instead of calling taskScaores.get(someKey) , you could use taskScores.getOrDefault(someKey, 0) .您可以使用taskScores.getOrDefault(someKey, 0)而不是调用taskScaores.get(someKey) ) 。

You can create iterate over the entries of defaultTaskScores with enhanced for -loop using entry set as a source, or by invoking forEach() method on the defaultTaskScores map directly.您可以使用条目集作为源,或通过在defaultTaskScores map 上直接调用forEach()方法,使用增强for循环创建对defaultTaskScores条目的迭代。

To update taskScores you can use Java 8 methods:要更新taskScores ,您可以使用 Java 8 种方法:

defaultTaskScores.forEach(taskScores::putIfAbsent);
defaultTaskScores.forEach((k, v) -> taskScores.computeIfAbsent(k, (key) -> v));

In case if you're not comfortable with lambda expressions and method references, have a look at this tutorial created by Oracle.如果您对 lambda 表达式和方法参考不满意,请查看由 Oracle 创建的本教程

Sidenote: avoid using obsolete double brace initialization, it creates an anonymous class under the hood and might cause memory leaks.旁注:避免使用过时的双括号初始化,它会在后台创建匿名 class 并可能导致 memory 泄漏。 Since Java 9 we have a family of overloaded factory methods Map.of() and method Map.ofEntries() which produce an immutable map . Since Java 9 we have a family of overloaded factory methods Map.of() and method Map.ofEntries() which produce an immutable map . When you need a map which is mutable like in this case, you can wrap it with a HashMap , like that:当您需要像这种情况一样可变的 map 时,您可以使用HashMap将其包装起来,如下所示:

Map<String, Integer> taskScores = new HashMap<>(Map.of(
    "Phalanxing", 90, "Shieldwalling",56,"Wedging",24
));

Only put in the Map those values if the key is not already there:如果密钥不存在,则仅在 Map 中输入这些值:

for(Map.Entry<String, Integer> entry : defaultTaskScores.entrySet()) {
  taskScores.putIfAbsent( entry.getKey(), entry.getValue() );
}

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

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