Java:Collection 接口(集合添加方法,集合 size()、remove() 和 removeAll() 方法的应用)

Collection 接口是 List 接口和 Set 接口的父接口,通常情况下不被直接使用。Collection 接口定义了一些通用的方法,通过这些方法可以实现对集合的基本操作。因为 List 接口和 Set 接口继承自 Collection 接口,所以也可以调用这些方法。

本节将介绍 Collection 接口中常用的方法,如表 1 所示。

表1 Collection接口的常用方法
方法名称说明
boolean add(E e)向集合中添加一个元素,E 是元素的数据类型
boolean addAll(Collection c)向集合中添加集合 c 中的所有元素
void clear()删除集合中的所有元素
boolean contains(Object o)判断集合中是否存在指定元素
boolean containsAll(Collection c)判断集合中是否包含集合 c 中的所有元素
boolean isEmpty()判断集合是否为空
Iterator<E>iterator()返回一个 Iterator 对象,用于遍历集合中的元素
boolean remove(Object o)从集合中删除一个指定元素
boolean removeAll(Collection c)从集合中删除所有在集合 c 中出现的元素
boolean retainAll(Collection c)仅仅保留集合中所有在集合 c 中出现的元素
int size()返回集合中元素的个数
Object[] toArray()返回包含此集合中所有元素的数组

例 1

经过前面的介绍,我们知道 Collection 是非常重要的一个接口,在表 1 中列出了其常用方法。本案例将编写一个简单的程序,演示如何使用 Collection 接口向集合中添加方法。具体实现代码如下:

  1. public static void main(Strmg[] args)
  2. {
  3. ArrayList list1=new ArrayList(); //创建集合 iist1
  4. ArrayList list2=new ArrayList(); //创建集合 Iist2
  5. list1.add("one"); //向 list1 添加一个元素
  6. list1.add("two"); //向 list1 添加一个元素
  7. list2.addAll(list1); //将 list1 的所有元素添加到 list2
  8. list2.add("three"); //向 list2 添加一个元素
  9. System.out.println("list2 集合中的元素如下:");
  10. Iterator it1=list2.iterator();
  11. while(it1.hasNext())
  12. {
  13. System.out.print(it1.next()+"、");
  14. }
  15. }

由于 Collection 是接口,不能对其实例化,所以上述代码中使用了 Collection 接口的 ArrayList 实现类来调用 Collection 的方法。add() 方法可以向 Collection 中添加一个元素,而调用 addAll() 方法可以将指定 Collection 中的所有元素添加到另一个 Collection 中。

代码创建了两个集合 list1 和 list2,然后调用 add() 方法向 list1 中添加了两个元素,再调用 addAll() 方法将这两个元素添加到 list2 中。接下来又向 list2 中添加了一个元素,最后输出 list2 集合中的所有元素。

运行结果如下:

list2 集合中的元素如下: one、two、three、

例 2

创建一个案例,演示 Collection 集合中 size()、remove() 和 removeAll() 方法的应用。具体代码如下:

  1. public static void main(String[] args)
  2. {
  3. ArrayList list1=new ArrayList(); //创建集合 list1
  4. ArrayList list2=new ArrayList(); //创建集合 list2
  5. list1.add("one");
  6. list1.add("two");
  7. list1.add("three");
  8. System.out.println("list1 集合中的元素数量:"+list1.size()); //输出list1中的元素数量
  9. list2.add("two");
  10. list2.add("four");
  11. list2.add("six");
  12. System.out.println("list2 集合中的元素数量:"+list2.size()); //输出list2中的元素数量
  13. list2.remove(2); //删除第 3 个元素
  14. System.out.println("\nremoveAll() 方法之后 list2 集合中的元素数量:"+list2.size());
  15. System.out.println("list2 集合中的元素如下:");
  16. Iterator it1=list2.iterator();
  17. while(it1.hasNext())
  18. {
  19. System.out.print(it1.next()+"、");
  20. }
  21. list1.removeAll(list2);
  22. System.out.println("\nremoveAll() 方法之后 list1 集合中的元素数量:"+list1.size());
  23. System.out.println("list1 集合中的元素如下:");
  24. Iterator it2=list1.iterator();
  25. while(it2.hasNext())
  26. {
  27. System.out.print(it2.next()+"、");
  28. }
  29. }

list2 集合在调用 remove(2) 方法删除第 3 个元素之后剩下了 two 和 four。list1.removeAll(list2) 语句会从 list1 中将 list1 和 list2 中相同的元素删除,即删除 two 元素。

运行结果如下:

list1 集合中的元素数量:3

Iist2 集合中的元素数量:3

remove() 方法之后

list2 集合中的元素数量:2

list2 集合中的元素如下: two、four、 removeAH() 方法之后

list1 集合中的元素数量:2

list1 集合中的元素如下: one、 three、


注意:retainAll() 方法的作用与 removeAll() 方法相反,即保留两个集合中相同的元素,其他全部删除。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

濯一一

你的鼓励:我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值