Collection接口的简述
Collection 接口是List、Set 和Queue 接口的父接口,该接口里定义的方法既可用于操作Set 集合,也可用于操作List 和Queue 集合。
Collection接口中方法的使用
添加
- add(Object obj):将元素objt、添加到集合中;
- addAll(addAll(Collection coll)):将一个集合的元素添加到当前集合中。
import java.util.ArrayList;
import java.util.Collection;
/**
* @author 宇戰天
* @description
* 1.add(Object obj)将元素e添加到集合中;
* 2.addAll(Collection coll)将一个集合的元素添加到当前集合中
* @create 2020-07-17-12:57
*/
public class AddTest {
public static void main(String[] args) {
//创建一个集合
Collection coll = new ArrayList();
// 1. add(Object obj)将元素e添加到coll中
coll.add(new Person("赵云",24));
coll.add(new Person("黄忠",56));
coll.add(new Person("关羽",34));
System.out.println(coll);
System.out.println("*******************************************************************");
// 2. addAll(Object coll1)将一个集合的元素添加到当前集合中
Collection coll1 = new ArrayList();
coll1.add(new Person("马超",22));
coll1.add(new Person("张飞",35));
coll1.add(new Person("刘备",45));
coll.addAll(coll1);
System.out.println(coll);
}
}
运行结果
获取有效元素的个数
- int size()
//创建一个集合
Collection coll = new ArrayList();
// 1. add(Object obj)将元素e添加到coll中
coll.add(new Person("赵云",24));
coll.add(new Person("黄忠",56));
coll.add(new Person("关羽",34));
System.out.println(coll);
//获取有效元素的个数
System.out.println(coll.size());
运行结果
清空集合和判断集合是否为空
- void clear();
- boolean isEmpty()
//创建一个集合
Collection coll = new ArrayList();
// 1. add(Object obj)将元素e添加到coll中
coll.add(new Person("赵云",24));
coll.add(new Person("黄忠",56));
coll.add(new Person("关羽",34));
System.out.println(coll);
//获取有效元素的个数
System.out.println(coll.size());
// 清空当前集合判断当前集合是否为空
coll.clear();
System.out.println(coll.isEmpty());
运行结果
是否包含某个元素
- contains(Object obj):判断当前集合中是否包含obj;
- containsAll(Collection c)判断当前集合中的所有元素是否存在在当前集合中
contains();
//创建一个集合
Collection coll = new ArrayList<>();
// 1. add(Object obj)将元素e添加到coll中
coll.add(new Person("赵云",24));
coll.add(new Person("黄忠",56));
coll.add(new Person("关羽",34));
boolean b = coll.contains(new Person("赵云",24));
System.out.println(b);
运行结果
containsAll();
//创建一个集合
Collection coll = new ArrayList();
// 1. add(Object obj)将元素e添加到coll中
coll.add(new Person("赵云",24));
coll.add(new Person("黄忠",56));
coll.add(new Person("关羽",34));
Collection coll1 = Arrays.asList(new Person("赵云",24),new Person("关羽",34));
boolean b = coll.containsAll(coll1);
System.out.println(b);
运行结果
删除
- remove(Object obj)从当前集合中移除obj元素,只会删除找到的第一个元素
- removeAll(Collection coll)取当前集合的差集直接修改原集合;
remove(Object obj)
//创建一个集合
Collection coll = new ArrayList();
// 1. add(Object obj)将元素e添加到coll中
coll.add(new Person("赵云",24));
coll.add(new Person("黄忠",56));
coll.add(new Person("关羽",34));
System.out.println("移除前");
System.out.println(coll);
coll.remove(new Person("赵云",24));
System.out.println("移除后");
System.out.println(coll);
运行结果
removeAll()
//创建一个集合
Collection coll = new ArrayList();
// 1. add(Object obj)将元素e添加到coll中
coll.add(new Person("赵云",24));
coll.add(new Person("黄忠",56));
coll.add(new Person("关羽",34));
System.out.println("移除前");
System.out.println(coll);
Collection coll1 = Arrays.asList(new Person("赵云",24),new Person("关羽",34));
coll.removeAll(coll1);
System.out.println("移除后");
System.out.println(coll);
运行结果
取两个集合的交集
boolean retainAll(Collection c):把交集的结果存在当前集合中,不影响c
//创建一个集合
Collection coll = new ArrayList();
// 1. add(Object obj)将元素e添加到coll中
coll.add(new Person("赵云",24));
coll.add(new Person("黄忠",56));
coll.add(new Person("关羽",34));
System.out.println("coll集合元素为");
System.out.println(coll);
Collection coll1 = Arrays.asList(new Person("马超",22),new Person("张飞",35),new Person("关羽",34));
System.out.println("coll1中元为");
System.out.println(coll1);
System.out.println("两个集合共有的元素为");
coll.retainAll(coll1);
System.out.println(coll);
运行结果为
注意
remove(),removeAll,retainAll()方法会直接修改 当前集合不会影响比较项集合中的元素。
两个集合是否相等
boolean equals(Object obj):比较两个集合中的元素是否完全相等
Collection coll = new ArrayList();
// 1. add(Object obj)将元素e添加到coll中
coll.add(new Person("赵云",24));
coll.add(new Person("黄忠",56));
coll.add(new Person("关羽",34));
Collection coll1 = Arrays.asList(new Person("马超",22),new Person("张飞",35),new Person("关羽",34));
System.out.println(coll);
System.out.println(coll1);
boolean equals = coll.equals(coll1);
System.out.println(equals);
运行结果
转成对象数组
Object[] toArray()
Collection coll = new ArrayList();
// 1. add(Object obj)将元素e添加到coll中
coll.add(new Person("赵云",24));
coll.add(new Person("黄忠",56));
coll.add(new Person("关羽",34));
Object[] objects = coll.toArray();
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i]+" ");
}
运行结果
将数组转换成集合
Arrays.asLIst();
List<String> objects1 = Arrays.asList(new String[]{"AA","BB","CC"});
//定义迭代器
Iterator<String> iterator = objects1.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}