在 Java 8 中使用 Inegers 列表处理嵌套 HashMap

问题描述 投票:0回答:1

我想在Java 8中做一个简单的操作。下面是代码

List<Integer> lst =  Arrays.asList(1,2,3);
Map<String,List> inner = new HashMap<String,List>();
inner.put("first",lst);

Map<String,Map<String,List>> outer = new HashMap<String,Map<String,List>>();
outer.put("Bigger",inner);

我想要做的是,如果子映射整数列表中的所有值都满足条件,则返回布尔值

我尝试执行以下操作,但似乎不起作用:

boolean lessThanfiveAll = outer.entrySet().stream().flatMap(e -> e.getValue().entrySet().stream().flatMap(v ->v.getValue().stream().allMatch(e -> e < 5)));

问题出现在

v ->v.getValue().stream().allMatch
allMatch 中的值不被视为整数,我收到编译器错误为
operator < cannot be applied to java.lang.Object

有人可以帮忙吗?

java-8 flatmap
1个回答
0
投票

需要使用

flatMap
吗?这行得通吗?

boolean lessThanfiveAll = outer.entrySet().stream().allMatch(e -> e.getValue().entrySet().stream().allMatch(v ->v.getValue().stream().allMatch(e1 -> e1 < 5)));

© www.soinside.com 2019 - 2024. All rights reserved.