集合交集,并集,差集运算

举例


class User {
    private Long cardId;
    private Long deviceId;

    public User(Long cardId, Long deviceId) {
        this.cardId = cardId;
        this.deviceId = deviceId;
    }

    public User() {
    }

    public Long getCardId() {
        return cardId;
    }

    public void setCardId(Long cardId) {
        this.cardId = cardId;
    }

    public Long getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(Long deviceId) {
        this.deviceId = deviceId;
    }

    @Override
    public String toString() {
        return "User{" +
                "cardId=" + cardId +
                ", deviceId=" + deviceId +
                '}';
    }

    //使用idea自动生成
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;
        User user = (User) o;
        return Objects.equals(cardId, user.cardId) &&
                Objects.equals(deviceId, user.deviceId);
    }

}

交集

    public static void main(String[] args) {
        List<User> a = new ArrayList<>();
        a.add(new User(1L, 1L));
        a.add(new User(2L, 2L));


        List<User> b = new ArrayList<>();
        b.add(new User(1L, 1L));
        b.add(new User(2L, 22L));

        System.out.println(a);//[User{cardId=1, deviceId=1}, User{cardId=2, deviceId=2}]
        a.retainAll(b);
        System.out.println(a);//[User{cardId=1, deviceId=1}]

    }

差集

    public static void main(String[] args) {
        List<User> a = new ArrayList<>();
        a.add(new User(1L, 1L));
        a.add(new User(2L, 2L));


        List<User> b = new ArrayList<>();
        b.add(new User(1L, 1L));
        b.add(new User(2L, 22L));

        System.out.println(a);//[User{cardId=1, deviceId=1}, User{cardId=2, deviceId=2}]
        a.removeAll(b);
        System.out.println(a);//[User{cardId=2, deviceId=2}]

    }

并集

    public static void main(String[] args) {
        List<User> a = new ArrayList<>();
        a.add(new User(1L, 1L));
        a.add(new User(2L, 2L));


        List<User> b = new ArrayList<>();
        b.add(new User(1L, 1L));
        b.add(new User(2L, 22L));

        System.out.println(a);//[User{cardId=1, deviceId=1}, User{cardId=2, deviceId=2}]
        a.addAll(b);
        System.out.println(a);//[User{cardId=1, deviceId=1}, User{cardId=2, deviceId=2}, User{cardId=1, deviceId=1}, User{cardId=2, deviceId=22}]

    }

并集操作有个问题,相同的元素会重复添加进来。可以先差集,再并集

    public static void main(String[] args) {
        List<User> a = new ArrayList<>();
        a.add(new User(1L, 1L));
        a.add(new User(2L, 2L));


        List<User> b = new ArrayList<>();
        b.add(new User(1L, 1L));
        b.add(new User(2L, 22L));

        System.out.println(a);//[User{cardId=1, deviceId=1}, User{cardId=2, deviceId=2}]
        a.removeAll(b);
        a.addAll(b);
        System.out.println(a);//[User{cardId=2, deviceId=2}, User{cardId=1, deviceId=1}, User{cardId=2, deviceId=22}]

    }

并集之前,必须先差集

源码

以差集为例,a.removeAll(b);

AbstractCollection

    public boolean removeAll(Collection<?> c) {
        Objects.requireNonNull(c);
        boolean modified = false;
        Iterator<?> it = iterator();
        while (it.hasNext()) {
            if (c.contains(it.next())) {
                it.remove();
                modified = true;
            }
        }
        return modified;
    }

调用contains判断

    public boolean contains(Object o) {
        Iterator<E> it = iterator();
        if (o==null) {
            while (it.hasNext())
                if (it.next()==null)
                    return true;
        } else {
            while (it.hasNext())
                if (o.equals(it.next()))
                    return true;
        }
        return false;
    }

调用equals比较元素,所以集合中的元素,必须重写equals方法

交集源码

    public boolean retainAll(Collection<?> c) {
        Objects.requireNonNull(c);
        boolean modified = false;
        Iterator<E> it = iterator();
        while (it.hasNext()) {
            if (!c.contains(it.next())) {
                it.remove();
                modified = true;
            }
        }
        return modified;
    }

并集

    public boolean addAll(Collection<? extends E> c) {
        boolean modified = false;
        for (E e : c)
            if (add(e))
                modified = true;
        return modified;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值