简体   繁体   English

从流的内部过滤器获取布尔值

[英]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 我能够把它分成小部分,因为我使用了两个过滤器,但是从内部过滤器我无法获得布尔值,因此我将得到所需的流,然后将使用flatmat(Collection :: stream)合并然后将转换为设置(以避免重复),因为两个列表都包含一个公共值,即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 我需要的输出必须是Integer和single set的形式,它们将被合并为[1,2,3,4,5,6],这里的值4是重复的,如果它必须限制结果集中的值,如果值小于然后提供了价值

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 我有一个简单的查询,因为我正在迭代map并且该map包含list作为值,这里我无法从内部filter获取boolean值

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 . 如果我理解正确,您需要首先展平地图列表,然后收集大于0的唯一值到Set

You can lead with map.values().stream() in order to get just values of the map. 您可以使用map.values().stream()来获取地图的值。

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/ ..谓词是一个可能是真或假的陈述,取决于值它的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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