package cn.itcast_02;
import java.util.ArrayList;
import java.util.Collection;
/*
* 1.添加功能
* boolean addAll(Collection c)添加一个集合的元素
* 2.删除功能
* boolean removeALl(Collection c)移除一个集合的元素(是一个还是所有)
* 3.判断功能
* boolean containsAll(Collection c)判断集合中是否包含指定的集合元素(是一个还是所有)
* 6.交集功能
* boolean retainAll(Collection c)两个集合都有的元素,思考元素去哪里了?返回的boolean又
* 是什么意思
* 移除此集合中未包含指定集合中所以元素
*/
public class Collegetion高级方法实现 {
public static void main(String[] args) {
//创建集合一
Collection c1 = new ArrayList();
c1.add("abc1");
c1.add("abc2");
c1.add("abc3");
c1.add("abc4");
//创建集合二
Collection c2 = new ArrayList();
c2.add("abc4");
c2.add("abc5");
c2.add("abc6");
c2.add("abc7");
System.out.println(c1);
System.out.println(c2);
//boolean addAll(Collection c)添加一个集合的元素
//c1.addAll(c2);
//System.out.println(c1);
//System.out.println(c2);
//boolean removeALl(Collection c)移除一个集合的元素
//只要有一个被移除就是移除
//System.out.println(c1.remove(c2));
System.out.println(c1);
System.out.println(c2);
// boolean containsAll(Collection c)判断集合中是否包含指定的集合元素(是一个还是所有)
//只要全部包含才是包含
System.out.println(c1.containsAll(c2));
System.out.println(c1);
System.out.println(c2);
// boolean retainAll(Collection c)两个集合都有的元素,思考元素去哪里了?返回的boolean又
是什么意思
//假设有两个集合A,B
//A对B做交集,最终的结果保存在A中,B不变
//返回值表示的是A是否发生过改变
System.out.println(c1.retainAll(c2));
System.out.println(c1);
System.out.println(c2);
}
}
Collegetion高级方法实现
最新推荐文章于 2025-04-10 16:33:23 发布