Java 收集HashMap<;字符串,对象>;来自Stream<;T>;
使用键的映射进行迭代并根据返回HashMap的条件,需要在代码下面收集返回映射 尝试在Java8中转换以下java代码Java 收集HashMap<;字符串,对象>;来自Stream<;T>;,java,Java,使用键的映射进行迭代并根据返回HashMap的条件,需要在代码下面收集返回映射 尝试在Java8中转换以下java代码 for (String key : sectionsJson.keySet()) { Map<String, Object> section = (Map<String, Object>) sectionsJson.get(key); if (index == (Integer) section.get
for (String key : sectionsJson.keySet()) {
Map<String, Object> section = (Map<String, Object>) sectionsJson.get(key);
if (index == (Integer) section.get(SECTION_FIELD_KEY_INDEX)) {
section.put(SECTION_FIELD_KEY_SECTION_KEY, key);
return section;
}
}
for(字符串键:sectionsJson.keySet()){
Map section=(Map)sectionsJson.get(key);
if(index==(整数)section.get(section\u FIELD\u KEY\u index)){
section.put(section\u FIELD\u KEY\u section\u KEY,KEY);
返回段;
}
}
任何建议。看起来您希望生成一个最多只有一个条目的
映射
Map<String,Object> map =
sectionsJson.entrySet()
.stream()
.filter(e -> {
Map<String, Object> section = e.getValue ();
return index == (Integer) section.get(SECTION_FIELD_KEY_INDEX);
}
.map(e -> new SimpleEntry<> (SECTION_FIELD_KEY_SECTION_KEY, e.getKey ()))
.limit(1)
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
Map=
sectionsJson.entrySet()
.stream()
.过滤器(e->{
Map section=e.getValue();
返回索引==(整数)section.get(section\u FIELD\u KEY\u index);
}
.map(e->newsimplentry(SECTION\u FIELD\u KEY\u SECTION\u KEY,e.getKey())
.限额(1)
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
看起来您的原始代码更简单
也许您可以简单地搜索所需的密钥:
String value =
sectionsJson.entrySet()
.stream()
.filter(e -> {
Map<String, Object> section = e.getValue ();
return index == (Integer) section.get(SECTION_FIELD_KEY_INDEX);
}
.map(Map.Entry::getKey)
.findFirst()
.orElse(null);
字符串值=
sectionsJson.entrySet()
.stream()
.过滤器(e->{
Map section=e.getValue();
返回索引==(整数)section.get(section\u FIELD\u KEY\u index);
}
.map(map.Entry::getKey)
.findFirst()
.orElse(空);
由于您正在生成一个Map
,它(最多)有一个值和一个常量键,因此该值是流管道应该搜索的唯一数据。根据您现有的代码。一旦找到匹配项,您就会返回映射。使用java 8也可以执行同样的操作
Optional<Integer> findAny = sectionsJson.keySet().stream().filter(key -> {
Map<String, Object> section = (Map<String, Object>)sectionsJson.get(key);
if (index == (Integer)section.get("SECTION_FIELD_KEY_INDEX")) {
section.put("SECTION_FIELD_KEY_SECTION_KEY", key);
return true;
}
return false;
}).findFirst();
if (findAny.isPresent()) {
System.out.println(sectionsJson.get(findAny.get()));
}
Optional findAny=sectionsJson.keySet().stream().filter(键->{
Map section=(Map)sectionsJson.get(key);
if(index==(整数)section.get(“section\u FIELD\u KEY\u index”)){
部分。put(“部分字段项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项项);
返回true;
}
返回false;
}).findFirst();
if(findAny.isPresent()){
System.out.println(sectionsJson.get(findAny.get());
}
根据您想要实现的目标,以下可能也是可行的解决方案:
简化循环
for (Map.Entry<String, Map<String, Object>> entry : sectionsJson.entrySet()) {
Map<String, Object> section = entry.getValue();
if (index == section.get(SECTION_FIELD_KEY_INDEX)) {
section.put(SECTION_FIELD_KEY_SECTION_KEY, entry.getKey());
return section;
}
}
// add the code for the case when no section was found
for(Map.Entry:sectionsJson.entrySet()){
Map section=entry.getValue();
if(index==section.get(section\u FIELD\u KEY\u index)){
section.put(section\u FIELD\u KEY\u section\u KEY,entry.getKey());
返回段;
}
}
//添加未找到节时案例的代码
分离流处理和元素变异
// find the section
Optional<Map.Entry<String, Map<String, Object>>> first = sectionsJson.entrySet().stream()
.filter(e -> (Integer) e.getValue().get(SECTION_FIELD_KEY_INDEX) == index)
.findFirst();
// mutate the section
if (first.isPresent()) {
Map.Entry<String, Map<String, Object>> sectionJson = first.get();
sectionJson.getValue().put(SECTION_FIELD_KEY_SECTION_KEY, sectionJson.getKey());
return sectionJson.getValue();
}
// add the code for the case when no section was found
//查找该节
可选的first=sectionsJson.entrySet().stream()
.filter(e->(整数)e.getValue().get(节字段键索引)=索引)
.findFirst();
//对该部分进行变异
if(first.isPresent()){
Map.Entry sectionJson=first.get();
sectionJson.getValue().put(SECTION_FIELD_KEY_SECTION_KEY,sectionJson.getKey());
返回sectionJson.getValue();
}
//添加未找到节时案例的代码
section.put(section\u FIELD\u KEY\u section\u KEY,k)
你的地图有副作用。(section
不是在lambda内部创建的,是共享的)。如果你在任何部分都找不到section\u FIELD\u KEY\u INDEX
,你会返回什么?感谢你的解决方案,它也可以从中学习其他方法。