如何迭代此结构 Map<String, Map<String, Set<String>>> 并将其值传递给方法?
How to iterate this structure Map<String, Map<String, Set<String>>> and pass its values into a method?
我想遍历此结构 Map<String, Map<String, Set<String>>>
并将其值传递到使用 Java Stream API.[= 的方法中14=]
假设我有一个函数
doSomething(String a, String b, Set c)
我需要为上面结构的值调用它。
Map<String, Map<String, Set<String>>> myMap = new HashMap<>();
Set<String> mySet1 = Set.of("A", "B");
Set<String> mySet2 = Set.of("C", "D");
Map<String, Set<String>> helperSet = new HashMap<>();
helperSet.put("1557491", mySet1);
helperSet.put("1557492", mySet2);
myMap.put("1165624", helperSet);
List<MyObject> details = myMap.entrySet().stream()
.map(e -> doSomething(
e.getValue().entrySet().stream()
.map(d -> d.getKey()).iterator().next(),
e.getKey(),
e.getValue().entrySet().stream()
.map(f -> f.getValue().iterator().next())
).collect(Collectors.toList());
这是我到目前为止所写的,不幸的是它不起作用,它只设法传递了第一组 (mySet1) 的值。
我对 Java 8 很陌生。非常感谢任何帮助!
您可以在一个流中使用一个流:
myMap.entrySet().stream()
.map(e1 -> e1.getValue().entrySet().stream()
.map(e2 -> doSomething(e1.getKey(), e2.getKey(), e2.getValue()))
.collect(toList())
).flatMap(List::stream)
.collect(toList());
我想遍历此结构 Map<String, Map<String, Set<String>>>
并将其值传递到使用 Java Stream API.[= 的方法中14=]
假设我有一个函数
doSomething(String a, String b, Set c)
我需要为上面结构的值调用它。
Map<String, Map<String, Set<String>>> myMap = new HashMap<>();
Set<String> mySet1 = Set.of("A", "B");
Set<String> mySet2 = Set.of("C", "D");
Map<String, Set<String>> helperSet = new HashMap<>();
helperSet.put("1557491", mySet1);
helperSet.put("1557492", mySet2);
myMap.put("1165624", helperSet);
List<MyObject> details = myMap.entrySet().stream()
.map(e -> doSomething(
e.getValue().entrySet().stream()
.map(d -> d.getKey()).iterator().next(),
e.getKey(),
e.getValue().entrySet().stream()
.map(f -> f.getValue().iterator().next())
).collect(Collectors.toList());
这是我到目前为止所写的,不幸的是它不起作用,它只设法传递了第一组 (mySet1) 的值。
我对 Java 8 很陌生。非常感谢任何帮助!
您可以在一个流中使用一个流:
myMap.entrySet().stream()
.map(e1 -> e1.getValue().entrySet().stream()
.map(e2 -> doSomething(e1.getKey(), e2.getKey(), e2.getValue()))
.collect(toList())
).flatMap(List::stream)
.collect(toList());