通过Stream流,根据属性对两个list集合求交集、差集

单个属性

交集

说明:使用stream().map().collect()方法提取出一个List中某个属性的集合,随后使用stream().filter(item->list.contains(args)).collect()方法,筛选出包含所提取属性的集合。

//两个list集合取交集
List<Student> intersection = newList.stream().filter(item -> oldList.stream()
                        .map(Student::getStudentCode).collect(Collectors.toList())
                        .contains(item.getStudentCode()))
                .collect(Collectors.toList());

打印值:[Student(id=1, name=张三, age=21), Student(id=3, name=王五, age=23)]

差集

说明:与求集的方法基本相同,在oldList前取反即可。此处对newList取差集,如需对oldList取差集,两个集合互换位置即可。

//两个list集合取差集
List<Student> difference = newList.stream().filter(item -> !oldList.stream()
                        .map(Student::getStudentCode).collect(Collectors.toList())
                        .contains(item.getStudentCode()))
                .collect(Collectors.toList());

打印值:[Student(id=2, name=李四, age=22)

多个属性

交集

说明:与单个属性求交集方法相同,可通过"&"拼接其余属性来实现

//多属性交集
List<Student> multIntersection = newList.stream().filter(item -> oldList.stream()
                    .map(stu->stu.getId()+"&"+stu.getAge()).collect(Collectors.toList())
                    .contains(item.getId()+"&"+item.getAge()))
              .collect(Collectors.toList());

打印值:[Student(id=1, name=张三, age=21), Student(id=3, name=王五, age=23)]

差集

说明:与多个属性交集相同,在oldList前取反即可

//多属性差集
List<Student> multDifference = newList.stream().filter(item -> !oldList.stream()
                      .map(stu->stu.getId()+"&"+stu.getAge()).collect(Collectors.toList())
                      .contains(item.getId()+"&"+item.getAge()))
              .collect(Collectors.toList());

打印值:[Student(id=2, name=李四, age=22)]

测试代码

@Data
@AllArgsConstructor
@NoArgsConstructor
class Student{
    //学号
    private String id;
    //姓名
    private String name;
    //年龄
    private String age;
}

public class Test {
    public static void main(String[] args) {

        List<Student> newList=new ArrayList<>();
        List<Student> oldList=new ArrayList<>();

        newList.add(new Student("1","张三","21"));
        newList.add(new Student("2","李四","22"));
        newList.add(new Student("3","王五","23"));

        oldList.add(new Student("1","张三","21"));
        oldList.add(new Student("3","赵六","23"));

        //单属性交集
        List<Student> intersection = newList.stream().filter(item -> oldList.stream()
                        .map(Student::getId).collect(Collectors.toList())
                        .contains(item.getId()))
                .collect(Collectors.toList());

        System.out.println(intersection);

        //单属性差集
        List<Student> difference = newList.stream().filter(item -> !(oldList.stream()
                        .map(Student::getId).collect(Collectors.toList()))
                        .contains(item.getId()))
                .collect(Collectors.toList());

        System.out.println(difference);
        
        //多属性交集
        List<Student> multIntersection = newList.stream().filter(item -> oldList.stream()
                        .map(stu->stu.getId()+"&"+stu.getAge()).collect(Collectors.toList())
                        .contains(item.getId()+"&"+item.getAge()))
                .collect(Collectors.toList());
        System.out.println(multIntersection);

        //多属性差集
        List<Student> multDifference = newList.stream().filter(item -> !oldList.stream()
                        .map(stu->stu.getId()+"&"+stu.getAge()).collect(Collectors.toList())
                        .contains(item.getId()+"&"+item.getAge()))
                .collect(Collectors.toList());
        System.out.println(multDifference);
    }
}

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值