Java 8 Java8流:过滤出一个映射
我试图找出如何在以下情况下使用Java8流: 假设我有以下地图:Java 8 Java8流:过滤出一个映射,java-8,Java 8,我试图找出如何在以下情况下使用Java8流: 假设我有以下地图: Map<String,String[]> map = { { "01": {"H01","H02","H03"}, {"11": {"B11","B12","B13"}} }; 我的尝试: map.entrySet().stream() // .flatMap(entry -> Arrays.stream(entry.getValue()) // .filter(channel
Map<String,String[]> map = { { "01": {"H01","H02","H03"}, {"11": {"B11","B12","B13"}} };
我的尝试:
map.entrySet().stream() //
.flatMap(entry -> Arrays.stream(entry.getValue()) //
.filter(channel -> Channel.isValid(channel))
.collect(Collectors.toMap()));
您当前的方法有几个问题
toMap()
的方法会导致编译错误T
并返回流
,而您正试图将映射作为返回值传递,这样也会导致编译错误Map<String, List<String>> resultSet = map.entrySet()
.stream() //
.flatMap(entry -> Arrays.stream(entry.getValue())
.filter(Channel::isValid)
.map(e -> new AbstractMap.SimpleEntry<>(entry.getKey(), e)))
.collect(Collectors.groupingBy(AbstractMap.SimpleEntry::getKey,
Collectors.mapping(AbstractMap.SimpleEntry::getValue,
Collectors.toList())));
以下是解决问题的另一种方法:
public static void stack2() {
Map<String, String[]> map = new HashMap<>();
map.put("01", new String[]{"H01", "H02", "H03"});
map.put("11", new String[]{"B11", "B12", "B13"});
Map<String, String[]> newMap = map.entrySet()
.stream()
.peek(e -> e.setValue(
Arrays.stream(e.getValue())
.filter(Main::stack2Filter)
.toArray(String[]::new)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
newMap.forEach((key, value) ->
System.out.println("key: " + key + " value " + Arrays.toString(value)));
}
public static boolean stack2Filter(String entry) {
return entry.equals("H02") || entry.equals("B11");
}
publicstaticvoidstack2(){
Map Map=newhashmap();
put(“01”,新字符串[]{“H01”、“H02”、“H03”});
put(“11”,新字符串[]{“B11”、“B12”、“B13”});
Map newMap=Map.entrySet()
.stream()
.peek(e->e.setValue(
Arrays.stream(例如getValue())
.filter(主::stack2Filter)
.toArray(字符串[]::new)))
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
newMap.forEach((键,值)->
System.out.println(“key:+key+“value”+Arrays.toString(value));
}
公共静态布尔stack2Filter(字符串条目){
返回条目。等于(“H02”)| |条目。等于(“B11”);
}
所以您一直想要第一个?不,是由筛选器确定的任意值。@Shvalb结果集的接收器类型是否应为Map
?或者它应该是Map
例如?@Aominè我会说,第二,很难在电话里解决这个问题…:)请你发布一个答案:嗯,我真的在考虑一些与收集器相关的东西。过滤作为收集器的下游收集器。groupingBy
这里…@Eugene但这篇文章用java-8:)标记,尽管我现在将研究java-9解决方案。@Eugene mhmm,正在考虑它。不确定是否可以使用收集器。筛选此处。。。但是我们可以在valueMapper中使用过滤器
,确保通道::是否有效
?可能应该为两个键添加一个合并函数。。。否则同意。@Eugene当然,我将使用方法引用,但在这种情况下,不应该有任何键冲突,因为根据定义,源(映射)不允许重复键。
map.entrySet()
.stream() //
.collect(Collectors.toMap(Map.Entry::getKey,
a -> Arrays.stream(a.getValue())
.filter(Channel::isValid)
.collect(Collectors.toList())));
public static void stack2() {
Map<String, String[]> map = new HashMap<>();
map.put("01", new String[]{"H01", "H02", "H03"});
map.put("11", new String[]{"B11", "B12", "B13"});
Map<String, String[]> newMap = map.entrySet()
.stream()
.peek(e -> e.setValue(
Arrays.stream(e.getValue())
.filter(Main::stack2Filter)
.toArray(String[]::new)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
newMap.forEach((key, value) ->
System.out.println("key: " + key + " value " + Arrays.toString(value)));
}
public static boolean stack2Filter(String entry) {
return entry.equals("H02") || entry.equals("B11");
}