JDK 容器概览 Collection

215 阅读1分钟

概览

Collection接口方法

基本操作

The Collection interface contains methods that perform basic operations, such as int size(), boolean isEmpty(), boolean contains(Object element), boolean add(E element), boolean remove(Object element), and Iterator iterator(). 大小,是否为空,增删查,迭代器。

批量操作

It also contains methods that operate on entire collections, such as boolean containsAll(Collection c), boolean addAll(Collection c), boolean removeAll(Collection c), boolean retainAll(Collection c), and void clear().

  • 批量增删查
  • 清空
  • 批量保留(俩集合交集)

容器转换成数组

Additional methods for array operations (such as Object[] toArray() and T[] toArray(T[] a) exist as well.

子接口

Set:集合

不能拥有重复元素

List :序列

有序容器,可以有重复元素,通常可以控制每一个元素的插入位置

Queue:队列

队列用来在对元素处理之前暂存元素,因此额外提供了一些插入,提取,检查的方法。队列的实现必须制定队列中元素的排序规则

Deque 双端队列

Collections 工具类

模板类 AbstarctColletion

模版类的很多操作都是基于迭代器

void add(E e); //未实现
void clear() ;//依赖迭代器的remove方法
boolean contains(Object o);
boolean remove(Object o);
abstract int size();
boolean isEmpty();
//bulk 依赖迭代器
boolean addAll(Collection<? extends E> c) ;
boolean containsAll(Collection<?> c);
boolean removeAll(Collection<?> c);//依赖迭代器的remove
boolean retainAll(Collection<?> c);//依赖迭代器的remove
//array
Object[] toArray();//依赖迭代器
T[] toArray(T[] a);//依赖迭代器
//迭代器
abstract Iterator<E> iterator();//抽象方法