Java 8 streams - passing a method to a filter

一世执手 提交于 2019-12-11 07:31:24

问题


I have a set of integers set2 and an object:

public class Bucket {
    private Integer id;
    private Set<Integer> set1;
...
}

I want to filter Buckets using streams, but only buckets where their set1 has intersection with another set2. I tried the following code:

Set<Bucket> selectedBuckets = allBuckets.stream()
    .filter(e -> Sets.intersection(e.getSet1(), set2).size()>1)
    .collect(Collectors.toSet());

But this would return all elements of allBuckets, instead of only the elements whose sets contains the intersection. How can I do this ?


回答1:


What you might just be looking for assuming you meant intersection(of Set set1 content) with any other Bucket in the List :

List<Bucket> allBuckets = new ArrayList<>(); // as you may initialise
Set<Bucket> selectedBuckets = allBuckets.stream()
        .filter(e -> allBuckets.stream()
                .filter(f -> f != e)
                .flatMap(b -> b.getSet1().stream())
                .anyMatch(s -> e.getSet1().contains(s)))
        .collect(Collectors.toSet());



回答2:


I found the error. Actually I was trying to reproduce using Integers instead of the object Bucket, like:

Set<Integer> set1 = new HashSet<>();
  set1.add(1);
  set1.add(2);
  set1.add(3);
  set1.add(4);

Set<Integer> set2 = new HashSet<>();
  set2.add(2);
  set2.add(3);
  set2.add(5);
  set2.add(6); 

Set<Integer> relevantBuckets = set1.stream()
            .filter(e -> Sets.intersection(set1, set2).size()>0)
            .collect(Collectors.toSet());

System.out.println(relevantBuckets);

prints: [1, 2, 3, 4]

When I reproduced using the object, it worked fine.

Set<Integer> allElements = new HashSet<>();
   allElements.add(1);
   allElements.add(2);
   allElements.add(3);
   allElements.add(4);
   allElements.add(5);

Set<Integer> set1 = new HashSet<>();
    set1.add(1);
    set1.add(2);

Set<Integer> set2 = new HashSet<>();
    set2.add(1);
    set2.add(20);

    Bucket bucket1 = new Bucket();
    bucket1.setId(1);
    bucket1.setMySet(set1);

    Bucket bucket2 = new Bucket();
    bucket2.setId(2);
    bucket2.setMySet(set2);

    Set<Bucket> allBuckets = new HashSet<>();
    allBuckets.add(bucket1);
    allBuckets.add(bucket2);


    Set<Bucket> selectedBuckets = allBuckets.stream()
            .filter(e -> Sets.intersection(e.getMySet(), allElements).size()>0)
            .collect(Collectors.toSet());

    System.out.println(selectedBuckets);

prints: [Bucket [id=1], Bucket [id=2]]

    selectedBuckets = allBuckets.stream()
            .filter(e -> Sets.intersection(e.getMySet(), allElements).size()>1)
            .collect(Collectors.toSet());

    System.out.println(selectedBuckets);

prints: [Bucket [id=1]]

I considered about deleting the question, but since the community could benefit from it, I resolved to answer in the question itself, instead of editing.



来源:https://stackoverflow.com/questions/54798311/java-8-streams-passing-a-method-to-a-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!