简体   繁体   中英

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 ie 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

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

这可能对你有帮助,谓词: http//howtodoinjava.com/java-8/how-to-use-predicate-in-java-8/ ..谓词是一个可能是真或假的陈述,取决于值它的变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM