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