集合框架知識系列02 集合頂層接口

首先,下面是Collection、Map和、Set接口的相關架構圖html

  1. Collection接口

clipboard.png

  1. Map接口

clipboard.png
圖片來源:http://www.cnblogs.com/skywan...架構

1、Collection相關接口和類

和Collection相關的接口主要有Collection、List和Set接口,其餘接口會在介紹三個接口中穿插講解。app

一、Collection接口

  • Collection是一個抽象出來的接口,定義以下:
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() {}
  • AbstractCollection抽象類繼承自Collection,實現了除iterator()和size()的全部方法。定義以下:
public abstract class AbstractCollection<E> implements Collection<E> {}

二、List接口

  • List接口繼承自Collection,List中的元素的容許重複的。定義以下:
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);
  • AbstractList抽象類繼承了AbstractCollection,而且實現了List接口,定義以下:
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {}

三、Set接口

  • Set接口繼承自Collection,Set是數學中定義的集合,元素不容許重複。定義以下:
public interface Set<E> extends Collection<E> {}

Set接口和Collection中方法一致,具體見Collection接口方法。htm

  • AbstractSet抽象類繼承了AbstractCollection,而且實現了Set接口,定義以下:
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {}

2、Map相關接口和類

Map是一種鍵值對的映射,沒有繼承Collection接口,具體定義以下:對象

public interface Map<K,V> {}

一、Map接口

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類

AbstractMap抽象類實現了Map接口,實現Map中定義的方法,定義以下:排序

public abstract class AbstractMap<K,V> implements Map<K,V> {}

三、SortedMap和NavigableMap接口

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相關接口

Iterator的工做是遍歷並選擇序列中的對象,它提供了一種訪問一個容器(container)對象中的各個元素,而又沒必要暴露該對象內部細節的方法。經過迭代器,開發人員不須要了解容器底層的結構,就能夠實現對容器的遍歷。因爲建立迭代器的代價小,所以迭代器一般被稱爲輕量級的容器。

一、Iterator接口

Iterator接口的定義以下:

public interface Iterator<E> {}

Iterator中定義的方法以下:

boolean hasNext();
E next();
//1.8新增
default void remove() {}
default void forEachRemaining(Consumer<? super E> action) {}

二、ListIterator接口

支持在迭代期間向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);

本節主要總結了集合相關的頂層接口,下一節將分析每一類集合實現類。

相關文章
相關標籤/搜索