n00b n00b - 1 year ago 71
Java Question

get boolean value from inner filter of stream

Consider I have following code

public static void main(String[] args) {
Map<String, List<String>> map = new HashMap<>();
map.put("a", Arrays.asList("1", "2", "3", "4"));
map.put("b", Arrays.asList("6", "7", "4", "5"));
map.entrySet().stream().filter(t -> t.getValue()
.stream().filter(v -> Integer.valueOf(v) > 0)

what I am trying to do is I have a map, whose values are as list of string.

These values will be converted into Integer and will be checked if the value is greater then the given value like in second/Inner filter.

I am able to break it into small parts as I have used two filters but from inner filter I am not able get boolean value so that I will get desired stream which will be then merged using flatmat(Collection::stream) and then will be converted to set(to avoid duplicacy) as both list contains a common value i.e 4

output I need is must be in form of Integer and single set which will be merged like [1,2,3,4,5,6] , here value 4 is duplicate and if it must restrict value from result set if value is less then provided value

I have a simple querry as I am iterating through map and that map contains list as value and here I am not able to fetch boolean value from inner

Answer Source

If I understand correctly, you need to flatten the lists of the map first, and then collect the unique values that are greater than 0 to a Set.

You can lead with map.values().stream() in order to get just values of the map.

Set<Integer> set = map.values().stream()
    .filter(num -> num > 0)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download