Process Nested HashMaps with List of Integers in Java 8

48 Views Asked by At

I want to do a simple Operation in Java 8. Below is the code

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);

What I want to do is , return boolean if all the values in a child map List of Integers satisfy a criteria

I tried to do below but it seems it is not working :

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

The issue arises at v ->v.getValue().stream().allMatch The value inside allMatch is not considered as an Integer and I get compiler error as operator < cannot be applied to java.lang.Object

Can anybody please help here ?

1

There are 1 best solutions below

0
user19286884 On

Do you need to use flatMap. Would this work?

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