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

柔光的暖阳◎ 2021-12-18 23:57 498阅读 0赞

举例

  1. class User {
  2. private Long cardId;
  3. private Long deviceId;
  4. public User(Long cardId, Long deviceId) {
  5. this.cardId = cardId;
  6. this.deviceId = deviceId;
  7. }
  8. public User() {
  9. }
  10. public Long getCardId() {
  11. return cardId;
  12. }
  13. public void setCardId(Long cardId) {
  14. this.cardId = cardId;
  15. }
  16. public Long getDeviceId() {
  17. return deviceId;
  18. }
  19. public void setDeviceId(Long deviceId) {
  20. this.deviceId = deviceId;
  21. }
  22. @Override
  23. public String toString() {
  24. return "User{" +
  25. "cardId=" + cardId +
  26. ", deviceId=" + deviceId +
  27. '}';
  28. }
  29. //使用idea自动生成
  30. @Override
  31. public boolean equals(Object o) {
  32. if (this == o) return true;
  33. if (!(o instanceof User)) return false;
  34. User user = (User) o;
  35. return Objects.equals(cardId, user.cardId) &&
  36. Objects.equals(deviceId, user.deviceId);
  37. }
  38. }

交集

  1. public static void main(String[] args) {
  2. List<User> a = new ArrayList<>();
  3. a.add(new User(1L, 1L));
  4. a.add(new User(2L, 2L));
  5. List<User> b = new ArrayList<>();
  6. b.add(new User(1L, 1L));
  7. b.add(new User(2L, 22L));
  8. System.out.println(a);//[User{cardId=1, deviceId=1}, User{cardId=2, deviceId=2}]
  9. a.retainAll(b);
  10. System.out.println(a);//[User{cardId=1, deviceId=1}]
  11. }

差集

  1. public static void main(String[] args) {
  2. List<User> a = new ArrayList<>();
  3. a.add(new User(1L, 1L));
  4. a.add(new User(2L, 2L));
  5. List<User> b = new ArrayList<>();
  6. b.add(new User(1L, 1L));
  7. b.add(new User(2L, 22L));
  8. System.out.println(a);//[User{cardId=1, deviceId=1}, User{cardId=2, deviceId=2}]
  9. a.removeAll(b);
  10. System.out.println(a);//[User{cardId=2, deviceId=2}]
  11. }

并集

  1. public static void main(String[] args) {
  2. List<User> a = new ArrayList<>();
  3. a.add(new User(1L, 1L));
  4. a.add(new User(2L, 2L));
  5. List<User> b = new ArrayList<>();
  6. b.add(new User(1L, 1L));
  7. b.add(new User(2L, 22L));
  8. System.out.println(a);//[User{cardId=1, deviceId=1}, User{cardId=2, deviceId=2}]
  9. a.addAll(b);
  10. System.out.println(a);//[User{cardId=1, deviceId=1}, User{cardId=2, deviceId=2}, User{cardId=1, deviceId=1}, User{cardId=2, deviceId=22}]
  11. }

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

  1. public static void main(String[] args) {
  2. List<User> a = new ArrayList<>();
  3. a.add(new User(1L, 1L));
  4. a.add(new User(2L, 2L));
  5. List<User> b = new ArrayList<>();
  6. b.add(new User(1L, 1L));
  7. b.add(new User(2L, 22L));
  8. System.out.println(a);//[User{cardId=1, deviceId=1}, User{cardId=2, deviceId=2}]
  9. a.removeAll(b);
  10. a.addAll(b);
  11. System.out.println(a);//[User{cardId=2, deviceId=2}, User{cardId=1, deviceId=1}, User{cardId=2, deviceId=22}]
  12. }

并集之前,必须先差集

源码

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

AbstractCollection

  1. public boolean removeAll(Collection<?> c) {
  2. Objects.requireNonNull(c);
  3. boolean modified = false;
  4. Iterator<?> it = iterator();
  5. while (it.hasNext()) {
  6. if (c.contains(it.next())) {
  7. it.remove();
  8. modified = true;
  9. }
  10. }
  11. return modified;
  12. }

调用contains判断

  1. public boolean contains(Object o) {
  2. Iterator<E> it = iterator();
  3. if (o==null) {
  4. while (it.hasNext())
  5. if (it.next()==null)
  6. return true;
  7. } else {
  8. while (it.hasNext())
  9. if (o.equals(it.next()))
  10. return true;
  11. }
  12. return false;
  13. }

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

交集源码

  1. public boolean retainAll(Collection<?> c) {
  2. Objects.requireNonNull(c);
  3. boolean modified = false;
  4. Iterator<E> it = iterator();
  5. while (it.hasNext()) {
  6. if (!c.contains(it.next())) {
  7. it.remove();
  8. modified = true;
  9. }
  10. }
  11. return modified;
  12. }

并集

  1. public boolean addAll(Collection<? extends E> c) {
  2. boolean modified = false;
  3. for (E e : c)
  4. if (add(e))
  5. modified = true;
  6. return modified;
  7. }

发表评论

表情:
评论列表 (有 0 条评论,498人围观)

还没有评论,来说两句吧...

相关阅读