集合的框架结构图
List、set和map对比
Collection接口的方法
//集合数量 | |
int size(); | |
//判断集合是否为空 | |
boolean isEmpty(); | |
//是否包含实例o | |
boolean contains(Object o); | |
//返回集合的迭代器 | |
Iterator<E> iterator(); | |
//返回集合的Object数组 | |
Object[] toArray(); | |
//返回集合的T数组 | |
<T> T[] toArray(T[] a); | |
//添加元素 | |
boolean add(E e); | |
//移除元素 | |
boolean remove(Object o); | |
//Returns true if this collection contains all of the elements in the specified collection. | |
boolean containsAll(Collection<?> c); | |
//Adds all of the elements in the specified collection to this collection (optional operation). | |
boolean addAll(Collection<? extends E> c); | |
//Removes all of this collection's elements that are also contained in the specified collection (optional operation). | |
boolean removeAll(Collection<?> c); | |
//过滤元素 | |
default boolean removeIf(Predicate<? super E> filter); | |
//保留集合c中的元素,即交集 | |
boolean retainAll(Collection<?> c); | |
//清空元素 | |
void clear(); | |
//Compares the specified object with this collection for equality. | |
boolean equals(Object o); | |
//任何重写Object.equals方法的类都必须重写Object.hashCode方法,以满足Object.hashCode方法的一般契约。其中, c1.equals(c2) 表示c1.hashCode()= = c2.hashCode()。 | |
int hashCode(); | |
//1.8可拆分的迭代器 | |
default Spliterator<E> spliterator() { | |
return Spliterators.spliterator(this, 0); | |
} | |
//Returns a sequential Stream with this collection as its source. | |
default Stream<E> stream() { | |
return StreamSupport.stream(spliterator(), false); | |
} | |
常用集合的分类
一、List
Collection 接口的接口 对象的集合(单列集合)
├——-List 接口:元素按进入先后有序保存,可重复
│—————-├ LinkedList 接口实现类, 链表, 插入删除, 没有同步, 线程不安全
│—————-├ ArrayList 接口实现类, 数组, 随机访问, 没有同步, 线程不安全
│—————-└ Vector 接口实现类 数组, 同步, 线程安全
│ ———————-└ Stack 是Vector类的实现类
二、set
Collection 接口的接口 对象的集合(单列集合)
└——-Set 接口: 仅接收一次,不可重复,并做内部排序
├—————-└HashSet 使用hash表(数组)存储元素
│————————└ LinkedHashSet 链表维护元素的插入次序
└ —————-TreeSet 底层实现为二叉树,元素排好序
三、Map
Map 接口 键值对的集合 (双列集合)
├———Hashtable 接口实现类, 同步, 线程安全
├———HashMap 接口实现类 ,没有同步, 线程不安全-
│—————–├ LinkedHashMap 双向链表和哈希表实现
│—————–└ WeakHashMap
├ ——–TreeMap 红黑树对所有的key进行排序
└———IdentifyHashMap