Java8的Stream根据Bean中的某一个属性,就两个List< Bean>的交集差集

Stream根据Bean中的某一个属性,就两个List< Bean>的交集差集

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liweifang
 * @desc
 * @date 2021-03-11 11:39
 */
class Student {

    private Integer age;

    private String haircolor;

    private Integer no;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getHaircolor() {
        return haircolor;
    }

    public void setHaircolor(String haircolor) {
        this.haircolor = haircolor;
    }

    public Integer getNo() {
        return no;
    }

    public void setNo(Integer no) {
        this.no = no;
    }

    public Student(Integer age, String haircolor, Integer no) {
        this.age = age;
        this.haircolor = haircolor;
        this.no = no;
    }


}

public class Testfilter {

    public static void main(String[] args) {

        Student s1 = new Student(11, "black", 456);
        Student s2 = new Student(12, "red", 123);//old

        Student s3 = new Student(11, "black", 123);
        Student s4 = new Student(12, "red1", 123);
        Student s5 = new Student(15, "white", 123);//new

        List<Student> oldList = new ArrayList<Student>() {{
            add(s1);
            add(s2);
        }};
        List<Integer> oldAgeList = oldList.stream().map(x -> x.getAge()).collect(Collectors.toList());

        List<Student> newList = new ArrayList<Student>() {{
            add(s3);
            add(s4);
            add(s5);
        }};
        List<Integer> newAgeList = oldList.stream().map(x -> x.getAge()).collect(Collectors.toList());

        //List<Bean> 根据Bean的一个属性求两个list的交集 差集
        //交集
        List<Student> updList = newList.stream()
                .filter(item -> oldList.stream()
                .map(e -> e.getAge())
                        .collect(Collectors.toList())
                        .contains(item.getAge()))
                .collect(Collectors.toList());
        System.out.println(updList);

        // 差集 (new - old)
        List<Student> addList = newList.stream()
                .filter(item -> !oldList.stream()
                        .map(e -> e.getAge() )
                        .collect(Collectors.toList())
                        .contains(item.getAge()))
                .collect(Collectors.toList());
        System.out.println(addList);

        // 差集 (old - new)
        List<Student> delList = oldList.stream()
                .filter(item -> !newList.stream()
                        .map(e -> e.getAge() )
                        .collect(Collectors.toList())
                        .contains(item.getAge()))
                .collect(Collectors.toList());
        System.out.println(delList);

        //List<Bean> 根据Bean的2个属性求两个list的交集 差集??

    }

}

最后留个问题,根据两个属性怎么求交集差集呢?如有朋友知道请不吝赐教

2022-05-17,根据评论修正了

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值