Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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
如何使用Java8流删除innermap密钥_Java_Dictionary_Java 8_Java Stream - Fatal编程技术网

如何使用Java8流删除innermap密钥

如何使用Java8流删除innermap密钥,java,dictionary,java-8,java-stream,Java,Dictionary,Java 8,Java Stream,我正在尝试学习Java流API,我有以下代码 List<HashMap<String, HashMap<String, String>>> map2 = new ArrayList<>(); HashMap<String, String> innerMap = new HashMap<>(); innerMap.put("a1", "hermione"); innerMap.put("a2", "harry"); inne

我正在尝试学习Java流API,我有以下代码

List<HashMap<String, HashMap<String, String>>> map2 = new ArrayList<>();

HashMap<String, String> innerMap = new HashMap<>();
innerMap.put("a1", "hermione");
innerMap.put("a2", "harry");
innerMap.put("a3", "ron");

HashMap<String, HashMap<String, String>> outermap = new HashMap<>();
outermap.put("friends", innerMap);

List<HashMap<String, HashMap<String, String>>> list = new ArrayList<>();
list.add(outermap);
List map2=new ArrayList();
HashMap innerMap=新的HashMap();
innerMap.put(“a1”,“赫敏”);
innerMap.put(“a2”,“harry”);
innerMap.put(“a3”、“ron”);
HashMap outermap=新HashMap();
outermap.put(“朋友”,innerMap);
列表=新的ArrayList();
列表。添加(outermap);

我想从内部地图中删除harry,并且应该保留相同的结构(
列表
),如何使用Streams API实现这一点?

对于这种要求,您应该添加地图的副本,而不是原始地图,如下所示:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, String> innerMap = new HashMap<>();
        innerMap.put("a1", "hermione");
        innerMap.put("a2", "harry");
        innerMap.put("a3", "ron");

        Map<String, Map<String, String>> outermap = new HashMap<>();
        outermap.put("friends", new HashMap<>(innerMap));

        List<Map<String, Map<String, String>>> list = new ArrayList<>();
        list.add(new HashMap<>(outermap));

        innerMap.remove("a2");
        System.out.println(innerMap);
        System.out.println(list);
    }
}
如果我添加了原始地图(如您所做的那样),从中删除条目也会影响列表。

您确实需要在此处使用,只要您只想删除某个地图条目,同时保留整个结构:

list.forEach(map -> map.forEach(
    (key, value) -> value.entrySet()
        .removeIf(entry -> "harry".equals(entry.getValue()))));
如果您坚持使用流API,只要流API适合处理集合,就要准备好处理条目和字典本身的复杂操作。看一看:

List<Map<String, Map<String, String>>> newList = list.stream()
    .map(outer -> outer.entrySet().stream()
        .map(inner -> new SimpleEntry<>(
            inner.getKey(),
            inner.getValue().entrySet().stream()
                .filter(entry -> !"harry".equals(entry.getValue()))
                .collect(Collectors.toMap(Entry::getKey, Entry::getValue))))
        .collect(Collectors.toMap(Entry::getKey, Entry::getValue)))
    .collect(Collectors.toList());
List newList=List.stream()
.map(外部->外部.entrySet().stream())
.map(内部->新建SimpleEntry(
internal.getKey(),
内部.getValue().entrySet().stream()
.filter(entry->!“harry”.equals(entry.getValue()))
.collect(Collectors.toMap(条目::getKey,条目::getValue)))
.collect(Collectors.toMap(条目::getKey,条目::getValue)))
.collect(Collectors.toList());

此外,如前所述,类更适合表示如此复杂的数据结构。

Streams不会修改底层对象,而是根据您的操作为您提供一个新对象。作为旁注,我建议您使用类来建模数据,而不是复杂的,嵌套映射到表示朋友列表。遵循OOP是一种理想的方法,它会让你的编程生活变得更好。我认为OP只是展示数据结构的意思是
List
,并希望使用
List
@Eklavya删除内部地图数据-他没有提到类似的事情。他提到了
Stream
,但是
Aniket Sahrawat
已经对其进行了适当的评论。是的,他想使用Stream over
list
,但您正试图直接处理
innermap
。此外,您不能确保始终使用“harry”和正确的键来正确引用地图(
“a2”
)。
List<Map<String, Map<String, String>>> newList = list.stream()
    .map(outer -> outer.entrySet().stream()
        .map(inner -> new SimpleEntry<>(
            inner.getKey(),
            inner.getValue().entrySet().stream()
                .filter(entry -> !"harry".equals(entry.getValue()))
                .collect(Collectors.toMap(Entry::getKey, Entry::getValue))))
        .collect(Collectors.toMap(Entry::getKey, Entry::getValue)))
    .collect(Collectors.toList());