Collection集合
Collection:是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的。
Collection的功能概述:
1:添加功能
booleanadd(Object obj):添加一个元素
booleanaddAll(Collection c):添加一个集合的元素
2:删除功能
voidclear():移除所有元素
boolean remove(Object o):移除一个元素
boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
3:判断功能
boolean contains(Object o):判断集合中是否包含指定的元素
boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
boolean isEmpty():判断集合是否为空
4:获取功能
Iterator<E> iterator()(重点) :迭代器,集合的专用遍历方式
5:长度功能
intsize():元素的个数
面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?前两个有,后一个是size方法。
6:交集功能
booleanretainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
7:把集合转换为数组,可以实现集合的遍历
Object[]toArray()
Collection接口概述
Collection 层次结构中的根接口。Collection表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。
Collection接口成员方法
boolean add(E e)
booleanremove(Object o)
void clear()
booleancontains(Object o)
booleanisEmpty()
int size()
booleanaddAll(Collection c)
booleanremoveAll(Collection c)
booleancontainsAll(Collection c)
booleanretainAll(Collection c):对交集的处理
Object[]toArray():把集合转成数组,可以实现集合的遍历
Iteratoriterator():迭代器,集合的专用遍历方式