Digao
Digao

Reputation: 560

Java 8 streams - passing a method to a filter

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 ?

Upvotes: 1

Views: 499

Answers (2)

Digao
Digao

Reputation: 560

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.

Upvotes: 0

Naman
Naman

Reputation: 31868

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

Upvotes: 2

Related Questions