user6833701
user6833701

Reputation:

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)
            .**WhatShouldIputThereToMakeitBolean**)
            .collect(Collectors.toSet());
}

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 query 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 filter

Upvotes: 4

Views: 806

Answers (2)

castletheperson
castletheperson

Reputation: 33486

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()
    .flatMap(Collection::stream)
    .map(Integer::valueOf)
    .filter(num -> num > 0)
    .collect(Collectors.toSet());

Upvotes: 3

Sudha Velan
Sudha Velan

Reputation: 633

This might help you, Predicate: http://howtodoinjava.com/java-8/how-to-use-predicate-in-java-8/.. predicate is a statement that may be true or false depending on the values of its variables.

Upvotes: 0

Related Questions