Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Map.computeiPresent()在Java中初始化列表_Java_Java 8_Maps - Fatal编程技术网

Map.computeiPresent()在Java中初始化列表

Map.computeiPresent()在Java中初始化列表,java,java-8,maps,Java,Java 8,Maps,我有以下代码: for (String val: values) { EnumType type = EnumType.get(val); if (type != null) { String key = type.name(); if (key.equals("camp2")) key = "camp1"; ArrayList<String> tempList= mapN.ge

我有以下代码:

for (String val: values) {
     EnumType type = EnumType.get(val);
     if (type != null) {
         String key = type.name();
         if (key.equals("camp2"))
                key = "camp1";
         ArrayList<String> tempList= mapN.get(key); //1
         if (tempList == null) {  // 2
            tempList = new ArrayList<>();
         }
         tempList.add(val);
         mapN.put(key, tempList);  //3
    }
}
for(字符串值:值){
EnumType类型=EnumType.get(val);
if(type!=null){
String key=type.name();
if(键等于(“camp2”))
key=“camp1”;
ArrayList tempList=mapN.get(key);//1
如果(templast==null){//2
templast=newarraylist();
}
圣殿骑士.add(val);
mapN.put(键,圣殿骑士);//3
}
}
其中,mapN和valies为:

private Map<String, ArrayList<String>> mapN
ArrayList<String>  values
type is an enum 
私有映射
数组列表值
类型是一个枚举
我有sonar,sonar告诉我,在//
1,2,3的值中,我需要使用:

Map.computeIfPresent()文件

但是我读过关于这个主题的书,我没有找到改变代码的方法


谁能帮我?

我想你可以把它缩短为:

values.forEach(val -> {
     EnumType type = EnumType.get(val);
     if(type != null){
          String key = type.name();
          if (key.equals("camp2"))
               key = "camp1";
          mapN.computeIfAbsent(key, x -> new ArrayList<>()).add(val);
     }
});
values.forEach(val->{
EnumType类型=EnumType.get(val);
if(type!=null){
String key=type.name();
if(键等于(“camp2”))
key=“camp1”;
computeIfAbsent(key,x->newArrayList()).add(val);
}
});

对@Eugene答案的修改版本得出:

values.forEach(val -> Optional.of(val)
   // get corresponding type, if it exists
   .map(EnumType::get)
   // get key
   .map(EnumType::name)
   // handle key == "camp2" scenario
   .map(key -> key.equals("camp2")
     ? "camp1"
     : key
   )
   // add val to map value list
   .ifPresent(key -> mapN.computeIfAbsent(
     key,
     x -> new ArrayList<>()
   ).add(val))
});
values.forEach(val->Optional.of(val)
//获取相应的类型(如果存在)
.map(EnumType::get)
//拿到钥匙
.map(枚举类型::名称)
//句柄键==“camp2”场景
.map(key->key.equals(“camp2”)
?“营地1”
:键
)
//将val添加到映射值列表
.ifPresent(键->mapN.computeIfAbsent(
钥匙
x->new ArrayList()
).add(val))
});

对不起,我在for中使用了if,我忘了在我的问题中提交if:(@AlexanderOvalle,这并不能真正改变问题…请参阅edit@AlexanderOvalle你在你的代码库中尝试过那个代码吗?@AlexanderOvalle你不需要在这里使用lambdas。你可以坚持你的for循环。(Eugene和我不同意forEach的使用;我只想指出,如果你对lambda不满意,你就不必使用它)。同意舒适的部分,因为在这样的块中,lambda既不能提高代码的可读性,也不能提供任何性能增强。