天天看點

集合交集,并集,差集運算

舉例

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;
    }