首先,下面是Collection、Map和、Set接口的相关架构图html
图片来源:http://www.cnblogs.com/skywan...架构
和Collection相关的接口主要有Collection、List和Set接口,其余接口会在介绍三个接口中穿插讲解。app
public interface Collection<E> extends Iterable<E> {}
其中包括了集合的基本操做,包括:删除、添加、遍历、大小等。
Collection中定义方法以下:spa
int size(); boolean isEmpty(); boolean contains(Object o); Iterator<E> iterator(); Object[] toArray(); <T> T[] toArray(T[] a); boolean add(E e); boolean remove(Object o); boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); boolean removeAll(Collection<?> c); boolean retainAll(Collection<?> c); void clear(); boolean equals(Object o); int hashCode(); //1.8新增 default boolean removeIf(Predicate<? super E> filter){} default Spliterator<E> spliterator(){} default Stream<E> stream(){} default Stream<E> parallelStream() {}
public abstract class AbstractCollection<E> implements Collection<E> {}
public interface List<E> extends Collection<E> {}
和Collection不重合、List特有的方法以下:code
boolean addAll(int index, Collection<? extends E> c); default void replaceAll(UnaryOperator<E> operator) {} default void sort(Comparator<? super E> c) {} E get(int index); E set(int index, E element); void add(int index, E element); E remove(int index); int indexOf(Object o); int lastIndexOf(Object o); ListIterator<E> listIterator(); ListIterator<E> listIterator(int index); List<E> subList(int fromIndex, int toIndex);
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {}
public interface Set<E> extends Collection<E> {}
Set接口和Collection中方法一致,具体见Collection接口方法。htm
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {}
Map是一种键值对的映射,没有继承Collection接口,具体定义以下:对象
public interface Map<K,V> {}
Map接口中定义了添加、删除、遍历等相关方法,具体方法以下:blog
int size(); boolean isEmpty(); boolean containsKey(Object key); boolean containsValue(Object value); V get(Object key); V put(K key, V value); V remove(Object key); void putAll(Map<? extends K, ? extends V> m); void clear(); Set<K> keySet(); Collection<V> values(); Set<Map.Entry<K, V>> entrySet(); interface Entry<K,V> {} boolean equals(Object o); int hashCode(); //1.8新增 default V getOrDefault(Object key, V defaultValue) {} default void forEach(BiConsumer<? super K, ? super V> action) {} default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {} default V putIfAbsent(K key, V value) {} default boolean remove(Object key, Object value) {} default boolean replace(K key, V oldValue, V newValue) {} default V replace(K key, V value) {} default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {} default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {} default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {} default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction){}
AbstractMap抽象类实现了Map接口,实现Map中定义的方法,定义以下:排序
public abstract class AbstractMap<K,V> implements Map<K,V> {}
SortedMap表示一个有序的键值映射,排序的方式有两种:天然排序和指定比较强排序。插入有序的SortedMap的全部元素都必须实现Comparable接口,具体方法以下:继承
Comparator<? super K> comparator(); SortedMap<K,V> subMap(K fromKey, K toKey); SortedMap<K,V> headMap(K toKey); SortedMap<K,V> tailMap(K fromKey); K firstKey(); K lastKey(); Set<K> keySet(); Collection<V> values(); Set<Map.Entry<K, V>> entrySet(); NavigableMap是SortedMap接口的扩展,有针对给定搜索目标返回最接近匹配项的导航方法。具体方法以下: Map.Entry<K,V> lowerEntry(K key); K lowerKey(K key); Map.Entry<K,V> floorEntry(K key); K floorKey(K key); Map.Entry<K,V> ceilingEntry(K key); K ceilingKey(K key); Map.Entry<K,V> higherEntry(K key); K higherKey(K key); Map.Entry<K,V> firstEntry(); Map.Entry<K,V> lastEntry(); Map.Entry<K,V> pollFirstEntry(); Map.Entry<K,V> pollLastEntry(); NavigableMap<K,V> descendingMap(); NavigableSet<K> navigableKeySet(); NavigableSet<K> descendingKeySet(); NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive); NavigableMap<K,V> headMap(K toKey, boolean inclusive); NavigableMap<K,V> tailMap(K fromKey, boolean inclusive); SortedMap<K,V> subMap(K fromKey, K toKey); SortedMap<K,V> headMap(K toKey); SortedMap<K,V> tailMap(K fromKey);
Iterator的工做是遍历并选择序列中的对象,它提供了一种访问一个容器(container)对象中的各个元素,而又没必要暴露该对象内部细节的方法。经过迭代器,开发人员不须要了解容器底层的结构,就能够实现对容器的遍历。因为建立迭代器的代价小,所以迭代器一般被称为轻量级的容器。
Iterator接口的定义以下:
public interface Iterator<E> {}
Iterator中定义的方法以下:
boolean hasNext(); E next(); //1.8新增 default void remove() {} default void forEachRemaining(Consumer<? super E> action) {}
支持在迭代期间向List中添加或删除元素,而且能够在List中双向滚动。定义以下:
public interface ListIterator<E> extends Iterator<E> {}
ListIterator中定义的方法以下:
boolean hasNext(); E next(); boolean hasPrevious(); E previous(); int nextIndex(); int previousIndex(); void remove(); void set(E e); void add(E e);
本节主要总结了集合相关的顶层接口,下一节将分析每一类集合实现类。