简体   繁体   English

Java 8流-将方法传递给过滤器

[英]Java 8 streams - passing a method to a filter

I have a set of integers set2 and an object: 我有一组整数set2和一个对象:

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. 我想使用流过滤存储桶,但仅使用set1与另一个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. 但这将返回allBuckets的所有元素,而不是仅返回其集合包含交集的元素。 How can I do this ? 我怎样才能做到这一点 ?

What you might just be looking for assuming you meant intersection(of Set set1 content) with any other Bucket in the List : 假设您的意思是Set set1的内容)与List任何其他Bucket交集,则可能要查找的内容

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

I found the error. 我发现了错误。 Actually I was trying to reproduce using Integers instead of the object Bucket, like: 实际上,我试图使用Integers而不是对象Bucket进行复制,例如:

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] 打印:[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]] 打印:[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]] 打印:[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. 我考虑过删除问题,但是由于社区可以从中受益,因此我决定在问题本身中回答,而不是进行编辑。

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

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