Collection集合做为集合的顶级接口你又对它了解多少呢?一般开发中主要用到它的子类,其使用相对较少,不过作为顶级接口还是有学习的必要!
1.基本概念
Collection:集合中的根接口,此接口的任何直接实现由更具体的子接口(Set,List)实现。
2.层次结构
图示:
3.常用方法
1):添加功能
//确保此collection包含指定的元素
boolean add(E e)
//将指定collection中的所有元素都添加到此collection 中
boolean addAll(Collection<? extends E> c)
/**
*用户类
*/
public class User {
String name;
int age;
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
/...get(),set()方法.../
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
public class Test {
public static void main(String[] args) {
//由于Collection是接口,所以需用子类对象去实现
Collection collection=new ArrayList();
//添加元素
collection.add(new User("一一", 11));
collection.add(new User("二二",22));
System.out.println(collection);//[User [name=一一, age=11], User [name=二二, age=22]]
Collection collection2=new ArrayList();
collection.add(new User("三三", 33));
collection.add(new User("四四",44));
//添加集合
collection.addAll(collection2);
System.out.println(collection);
//[User [name=一一, age=11], User [name=二二, age=22], User [name=三三, age=33], User [name=四四, age=44]]
}
}
注:集合不能添加基本数据类型。
2):删除功能
// 移除此 collection 中的所有元素
void clear()
//从此collection中移除指定元素的单个实例
boolean remove(Object o)
//移除此collection中那些也包含在指定collection中的所有元素
boolean removeAll(Collection<?> c)
Collection collection = new ArrayList();
User user = new User("一一", 11);
User user2 = new User("二二", 22);
// 添加元素
collection.add(user);
collection.add(user2);
System.out.println(collection);// [User [name=一一, age=11], User [name=二二, age=22]]
// 删除某个元素对象
// collection.remove(user);
// System.out.println(collection);//[User [name=二二, age=22]]
Collection collection2 = new ArrayList();
collection2.add(user);
collection2.add(user2);
// 删除集合中指定元素
collection.removeAll(collection2);
System.out.println(collection);//[]
System.out.println(collection2);// [User [name=一一, age=11], User [name=二二, age=22]]
//清空
collection2.clear();
System.out.println(collection2);//[]
3):判断功能
//collection不包含元素,则返回true
boolean isEmpty()
//此collection包含指定的元素,则返回true
boolean contains(Object o)
//此collection包含指定collection中的所有元素,则返回true
boolean containsAll(Collection<?> c)
Collection collection = new ArrayList();
User user = new User("一一", 11);
User user2 = new User("二二", 22);
collection.add(user);
collection.add(user2);
Collection collection2 = new ArrayList();
collection2.add(user);
collection2.add(user2);
//判断是否为空
boolean b=collection.isEmpty();
System.out.println(b);//false
//判断子元素
boolean b=collection.contains(user);
boolean bb=collection.contains(new User("一一", 11));
System.out.println(b);//true
System.out.println(bb);//false
//判断集合
boolean b2=collection.containsAll(collection2);
System.out.println(b2);//true
4):长度功能
//返回此collection中的元素数
int size()
int size=collection.size();
System.out.println(size);//2
5):求交集
// 仅保留此collection中那些也包含在指定collection的元素
// collection由于调用而发生更改,则返回true
boolean retainAll(Collection<?> c)
Collection collection = new ArrayList();
User user = new User("一一", 11);
User user2 = new User("二二", 22);
collection.add(user);
collection.add(user2);
Collection collection2 = new ArrayList();
collection2.add(user);
collection2.add(user2);
Collection collection3=new ArrayList();
//由于集合没有更改而返回false
System.out.println(collection.retainAll(collection2));//false
System.out.println(collection);//[User [name=一一, age=11], User [name=二二, age=22]]
//由于集合发生更改而返回false
System.out.println(collection.retainAll(collection3));//true
System.out.println(collection);//[]
6):把集合转数组
// 返回包含此collection中所有元素的数组
Object[] toArray()
//返回包含此collection中所有元素的数组,返回数组的运行时类型与指定数组的运行时类型相同
T[] toArray(T[] a)
Collection collection = new ArrayList();
collection.add(new User("一一", 11));
collection.add(new User("二二", 22));
Object[] objects=collection.toArray();
for (int i = 0; i < objects.length; i++) {
System.out.println((User)objects[i]);
//User [name=一一, age=11]
//User [name=二二, age=22]
}
4.遍历集合
1)转数组
使用toArray()方法
2)使用迭代器
//这是集合特有的方式
Collection collection = new ArrayList();
collection.add(new User("一一", 11));
collection.add(new User("二二", 22));
//返回在此collection的元素上进行迭代的迭代器
Iterator iterator=collection.iterator();
//判断是否还有下一个
while (iterator.hasNext()) {
//返回迭代的下一个元素
System.out.println(iterator.next());
//User [name=一一, age=11]
//User [name=二二, age=22]
}
5.集合和数组的区别
集合:长度不固定、只能存储引用数据类型、可以同时存储不同类型数据;
数组:长度固定、可以存储引用数据类型和基本数据类型、只能存储同一类型数据;
注:Collection集合使用较少。