Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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
Java 带条件的嵌套HashMap上的流_Java_Java 8_Java Stream - Fatal编程技术网

Java 带条件的嵌套HashMap上的流

Java 带条件的嵌套HashMap上的流,java,java-8,java-stream,Java,Java 8,Java Stream,假设我们有一些Map结构,如下所示: Map<Type, Map<String, String>> outterMap = new HashMap<>(); Map<String, String> innerMap1 = new HashMap<>(); innerMap1.put("1", "test1"); innerMap1.put("2", "test2"); innerMap1.put("3", "test3"); inne

假设我们有一些
Map
结构,如下所示:

Map<Type, Map<String, String>> outterMap = new HashMap<>();
Map<String, String> innerMap1 = new HashMap<>();

innerMap1.put("1", "test1");
innerMap1.put("2", "test2");
innerMap1.put("3", "test3");
innerMap1.put("4", "test4");

Map<String, String> innerMap2 = new HashMap<>();

innerMap2.put("5", "test5");
innerMap2.put("6", "test6");
innerMap2.put("3", "test7");

outterMap.put(Type.TEXT, innerMap1);
outterMap.put(Type.INTEGER, innerMap2);
控制台上的输出如下所示:

for (Type type : outterMap.keySet()) {
    for (String value : outterMap.get(type).values()) {
        if(type.equals(Type.TEXT)) {
            System.out.println("TEXT: " + value);
        }else if(type.equals(Type.INTEGER)) {
            System.out.println("INTEGER: " + value);
        }
    }
}
TEXT: test1
TEXT: test2
TEXT: test3
TEXT: test4
INTEGER: test7
INTEGER: test5
INTEGER: test6
outterMap.keySet().stream().forEach(type -> {
            outterMap.get(type)
            .values()
            .stream()
            .forEach(value -> {
                if(type.equals(Type.TEXT)) {
                    System.out.println("TEXT: " + value);
                } else if (type.equals(Type.INTEGER)) {
                    System.out.println("INTEGER: " + value);
                }
            });
        });
是否有任何选项可以在流的帮助下编写它。我可以将stream与lambda一起使用,它看起来是这样的:

for (Type type : outterMap.keySet()) {
    for (String value : outterMap.get(type).values()) {
        if(type.equals(Type.TEXT)) {
            System.out.println("TEXT: " + value);
        }else if(type.equals(Type.INTEGER)) {
            System.out.println("INTEGER: " + value);
        }
    }
}
TEXT: test1
TEXT: test2
TEXT: test3
TEXT: test4
INTEGER: test7
INTEGER: test5
INTEGER: test6
outterMap.keySet().stream().forEach(type -> {
            outterMap.get(type)
            .values()
            .stream()
            .forEach(value -> {
                if(type.equals(Type.TEXT)) {
                    System.out.println("TEXT: " + value);
                } else if (type.equals(Type.INTEGER)) {
                    System.out.println("INTEGER: " + value);
                }
            });
        });
可能是这样的:

outterMap.keySet()
         .stream()
         .flatMap(x -> outterMap.getOrDefault(x, Collections.emptyMap())
                                .values()
                                .stream()
                                .map(y -> new SimpleEntry<>(x, y)))
         .forEachOrdered(entry -> {
               System.out.println(entry.getKey() + "  " + entry.getValue());
         });
outterMap.keySet()
.stream()
.flatMap(x->outterMap.getOrDefault(x,Collections.emptyMap())
.values()
.stream()
.map(y->newsimplentry(x,y)))
.forEachOrdered(条目->{
System.out.println(entry.getKey()+“”+entry.getValue());
});

但是它的可读性远远低于您所拥有的。

您可以在外部
Map#entrySet
上进行流式处理,获取每个条目并打印出外部
Map的键。
forEach()回调中的entry
和内部
Map

outterMap.entrySet()
         .stream()
         .forEach(e -> e.getValue()
                        .values()
                        .forEach(v -> System.out.println(e.getKey()+ " " + v)));
输出:

TEXT test1
TEXT test2
TEXT test3
TEXT test4
INTEGER test7
INTEGER test5
INTEGER test6
那怎么办

outterMap.keySet().forEach(type -> outterMap.get(type)
        .values()
        .stream()
        .map(value -> transform(type, value))
        .forEach(System.out::println));


这里不需要使用流。For循环更简单。旁白:不要使用
outterMap.get(type)
,而是使用
outterMap.entrySet()
而不是
keySet()
,并将键和值一起获取。对于当前用例,
outerMap.forEach((type,innerMap)->innerMap.values().stream().map(value->->type.equals(type.TEXT)“::“INTEGER:”+value).forEach(System.out::println));
。当然,如果有多个枚举值,您可以使用实用程序方法返回字符串的前缀并调用它。@Naman或只使用
outerMap.forEach((type,innerMap)->innerMap.values().forEach(value->System.out.println(type.name()+”:“+value)))
在映射的键上进行流式处理并在同一映射上调用
getOrDefault
有什么意义?键应该始终存在,并且当您在
entrySet
上迭代时,您甚至根本不需要查找。