stream流:根据某个字段取交集、差集、集合的举例

java stream 根据某个字段取交集、差集、集合的举例

在Java中,Stream是一种用于处理集合数据的强大工具。它提供了一种函数式编程的方式来表达复杂的数据处理操作,例如过滤、映射、排序等。

本文将介绍如何使用Stream根据一个对象的字段取两个List的交集。我们将使用Java 8及以上版本的Stream API来完成这个任务。


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


anyMatch(交集)

XYing

判断数据列表中是否存在任意一个元素符合设置的predicate条件,如果是就返回true,否则返回false。

接口定义

boolean anyMatch(Predicate<? super T> predicate);

方法描述

在anyMatch 接口定义中是接收 Predicate 类型参数,在Lamdba表达式中 Predicate 是接收一个T类型参数,然后经过逻辑验证返回布尔值结果。这里anyMatch表示,判断的条件里,任意一个元素符合条件,就返回true值。
使用场景:
两个集合的交集

noneMatch(差集)

判断数据列表中全部元素都不符合设置的predicate条件,如果是就返回true,否则返回false,流为空时总是返回true。

接口定义

boolean noneMatch(Predicate<? super T> predicate);

方法描述

在noneMatch接口定义中是接收 Predicate 类型参数,在Lamdba表达式中 Predicate 是接收一个T类型参数,然后经过逻辑验证返回布尔值结果。这里noneMatch表示与allMatch相反,判断的条件里的元素,所有的元素都不符合,就返回true值。
适用场景:
两个集合的差集

提示:以下是本篇文章正文内容,下面案例可供参考

stream 交集

List<String> list1 = new ArrayList();
list1.add("1111");
list1.add("2222");
list1.add("3333");
 
List<String> list2 = new ArrayList();
list2.add("3333");
list2.add("4444");
list2.add("5555");
// 交集
List<String> list= list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---得到交集 intersection---");
list.parallelStream().forEach(System.out :: println);

List<Person> list1 = new ArrayList<>();
list1.add(new Person(1, "Alice"));
list1.add(new Person(2, "Bob"));
list1.add(new Person(3, "Charlie"));

List<Person> list2 = new ArrayList<>();
list2.add(new Person(2, "Bob"));
list2.add(new Person(4, "David"));
list2.add(new Person(5, "Eve"));

// 取交集  A∩B,以Id相等作为判断依据,并且将集合B中以A集合为例补全作为新的集合
List<Person> list= list1.stream()
        .filter(p -> list2.stream().anyMatch(q -> q.getId() == p.getId()))
        .collect(Collectors.toList());
list.forEach(System.out::println);


stream 差集

List<String> list1 = new ArrayList();
list1.add("1111");
list1.add("2222");
list1.add("3333");
 
List<String> list2 = new ArrayList();
list2.add("3333");
list2.add("4444");
list2.add("5555");

// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
System.out.println("---得到差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
 
// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
System.out.println("---得到差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);


List<Person> list1 = new ArrayList<>();
list1.add(new Person(1, "Alice"));
list1.add(new Person(2, "Bob"));
list1.add(new Person(3, "Charlie"));

List<Person> list2 = new ArrayList<>();
list2.add(new Person(2, "Bob"));
list2.add(new Person(4, "David"));
list2.add(new Person(5, "Eve"));

// 取差集 A\B,以Id的作为判断依据,元素属于集合A,但是不属于集合B的元素拿出来作为新的集合
List<Person> list= list1.stream().filter(p -> list2.stream()
          .noneMatch(q -> q.getId() == p.getId()))
          .collect(Collectors.toList());
list.forEach(System.out::println);

stream 并集

List<String> list1 = new ArrayList();
list1.add("1111");
list1.add("2222");
list1.add("3333");
 
List<String> list2 = new ArrayList();
list2.add("3333");
list2.add("4444");
list2.add("5555");

// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---得到并集 listAll---");
listAll.parallelStream().forEach(System.out :: println);
 
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEach(System.out :: printl


List<Person> list1 = new ArrayList<>();
list1.add(new Person(1, "Alice"));
list1.add(new Person(2, "Bob"));
list1.add(new Person(3, "Charlie"));

List<Person> list2 = new ArrayList<>();
list2.add(new Person(2, "Bob"));
list2.add(new Person(4, "David"));
list2.add(new Person(5, "Eve"));
// List1与 List2的交集  
// 过滤list1 ,遍历集合list2   
// anyMatch(Predicate p) 传入一个断言型函数,对流中所有的元素进行判断,只要有一个满足条件返回true,不满足false。 
// 取出list2中,符合判断条件的元素,返回值为true。将对象保存新集合中。
List<Person> intersectA = list1.stream().filter(
              a -> list2.stream().map(
              Person::getName).anyMatch(   // 流中是否有元素能匹配给定的谓词,有则返回true 否false。
              name -> Objects.equals(a.getName(), name)))
              .collect(Collectors.toList());               
// 遍历List
intersectA.parallelStream().forEach(System.out :: println);


Person

提示:Person :

public class Person {
    private int id;
    private String name;

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值