前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >集合运算之交集、并集、差集

集合运算之交集、并集、差集

作者头像
九转成圣
发布2024-04-10 16:52:11
620
发布2024-04-10 16:52:11
举报
文章被收录于专栏:csdncsdn

集合运算之交集、并集、差集

标签:集合

交集retainAll

retain /rɪˈteɪn/ 保留

list1.retainAll(list2)方法解释

Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection

list1仅仅保留list2中有的元素,没有的统统移出,注意这里修改了原集合list1

代码语言:javascript
复制
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
list2.add("c");
list2.add("d");
list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的交集");
boolean b = list1.retainAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
代码语言:javascript
复制
list1 = [a, b, c, c]
list2 = [c, d, e]
两个集合的交集
b = true
list1 = [c, c]
代码语言:javascript
复制
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
// list1.add("c");
// list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
list2.add("c");
list2.add("d");
list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的交集");
boolean b = list1.retainAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
代码语言:javascript
复制
list1 = [a, b]
list2 = [c, d, e]
两个集合的交集
b = true
list1 = []
代码语言:javascript
复制
List<String> list1 = new ArrayList<>();
// list1.add("a");
// list1.add("b");
list1.add("c");
list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
list2.add("c");
list2.add("d");
list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的交集");
boolean b = list1.retainAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
代码语言:javascript
复制
list1 = [c, c]
list2 = [c, d, e]
两个集合的交集
b = false
list1 = [c, c]

并集addAll

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it’s nonempty.)

代码语言:javascript
复制
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
list2.add("c");
list2.add("d");
list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的并集");
boolean b = list1.addAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
代码语言:javascript
复制
list1 = [a, b, c, c]
list2 = [c, d, e]
两个集合的并集
b = true
list1 = [a, b, c, c, c, d, e]
代码语言:javascript
复制
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
// list2.add("c");
// list2.add("d");
// list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的并集");
boolean b = list1.addAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
代码语言:javascript
复制
list1 = [a, b, c, c]
list2 = []
两个集合的并集
b = false
list1 = [a, b, c, c]

差集removeAll

Removes from this list all of its elements that are contained in the specified collection (optional operation).

代码语言:javascript
复制
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("c");
System.out.println("list1 = " + list1);
List<String> list2 = new ArrayList<>();
list2.add("c");
list2.add("d");
list2.add("e");
System.out.println("list2 = " + list2);
System.out.println("两个集合的差集");
boolean b = list1.removeAll(list2);
System.out.println("b = " + b);
System.out.println("list1 = " + list1);
代码语言:javascript
复制
list1 = [a, b, c, c]
list2 = [c, d, e]
两个集合的差集
b = true
list1 = [a, b]

注意事项:

list1.xxxAll(list2)

  1. list1可能会被修改了
  2. list1被修改了就返回true,否则返回false
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 集合运算之交集、并集、差集
    • 标签:集合
    • 交集retainAll
    • 并集addAll
    • 差集removeAll
    • 注意事项:
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档