在 android 項目開發過程中,總會使用到 Collection,對於一些基礎的使用方法還是可以的,但是涉及到較深層次的就有點力不從心了,所以打算開始徹底地學習一下 java 集合方面的知識點,做個記錄總結,
分析工具
JavaJDK:1.7.0_79
AndroidStudio:3.5
首先來看下 java 集合框架的總圖:
集合框架主要分爲兩大類: Collection 和 Map。
Collection
Collection 是 List、Set 等集合高度抽象出來的接口,它包含了這些集合的基本操作,它主要又分爲兩大部分:List和Set。
List 接口通常表示一個列表(數組、隊列、鏈表、棧等),其中的元素可以重複,常用實現類爲 ArrayList 和 LinkedList,另外還有不常用的 Vector。另外,LinkedList 還是實現了 Queue 接口,因此也可以作爲隊列使用。
Set 接口通常表示一個集合,其中的元素不允許重複(通過 hashcode 和 equals 函數保證),常用實現類有 HashSet 和 TreeSet,HashSet 是通過 Map 中的HashMap 實現的,而 TreeSet 是通過 Map 中的 TreeMap 實現的。另外,TreeSet 還實現了 SortedSet 接口,因此是有序的集合(集合中的元素要實現 Comparable 接口,並覆寫 Compartor 函數纔行)。
我們看到,抽象類 AbstractCollection、AbstractList 和 AbstractSet 分別實現了Collection、List 和 Set 接口,這就是在 Java 集合框架中用的很多的適配器設計模式,用這些抽象類去實現接口,在抽象類中實現接口中的若干或全部方法,這樣下面的一些類只需直接繼承該抽象類,並實現自己需要的方法即可,而不用實現接口中的全部抽象方法。
AbstractList繼承AbstractCollection實現List
/**
* {@code AbstractList} is an abstract implementation of the {@code List} interface, optimized
* for a backing store which supports random access. This implementation does
* not support adding or replacing. A subclass must implement the abstract
* methods {@code get()} and {@code size()}, and to create a
* modifiable {@code List} it's necessary to override the {@code add()} method that
* currently throws an {@code UnsupportedOperationException}.
*
* @since 1.2
*/
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {}
AbstractSet 繼承AbstractCollection實現Set
/**
* An AbstractSet is an abstract implementation of the Set interface. This
* implementation does not support adding. A subclass must implement the
* abstract methods iterator() and size().
*
* @since 1.2
*/
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {}
AbstractCollection實現Collection
/**
* Class {@code AbstractCollection} is an abstract implementation of the {@code
* Collection} interface. A subclass must implement the abstract methods {@code
* iterator()} and {@code size()} to create an immutable collection. To create a
* modifiable collection it's necessary to override the {@code add()} method that
* currently throws an {@code UnsupportedOperationException}.
*
* @since 1.2
*/
public abstract class AbstractCollection<E> implements Collection<E>{}
Map
Map 是一個映射接口,其中的每個元素都是一個 key-value 鍵值對,同樣抽象類 AbstractMap 通過適配器模式實現了 Map 接口中的大部分函數,TreeMap、HashMap、WeakHashMap 等實現類都通過繼承 AbstractMap 來實現,另外,不常用的 HashTable 直接實現了 Map 接口,它和 Vector 都是 JDK1.0 就引入的集合類。
TreeMap繼承AbstractMap實現NavigableMap
/*
* @since 1.2
*/
public class TreeMap<K, V> extends AbstractMap<K, V>
implements SortedMap<K, V>, NavigableMap<K, V>, Cloneable, Serializable {}
NavigableMap繼承SortedMap
public interface NavigableMap<K,V> extends SortedMap<K,V> {}
SortedMap接口繼承Map接口
/**
* A map that has its keys ordered. The sorting is according to either the
* natural ordering of its keys or the ordering given by a specified comparator.
*/
public interface SortedMap<K,V> extends Map<K,V> {}
HashMap繼承AbstractMap
public class HashMap<K, V> extends AbstractMap<K, V> implements Cloneable, Serializable {}
WeakHashMap繼承AbstractMap
/*
* @since 1.2
* @see HashMap
* @see WeakReference
*/
public class WeakHashMap<K, V> extends AbstractMap<K, V> implements Map<K, V> {}
Iterator
Iterator 是遍歷集合的迭代器(不能遍歷 Map,只用來遍歷 Collection),Collection 的實現類都實現了 iterator() 函數,它返回一個 Iterator 對象,用來遍歷集合,ListIterator 則專門用來遍歷 List。而 Enumeration 則是 JDK1.0 時引入的,作用與 Iterator 相同,但它的功能比 Iterator 要少,它只能再 Hashtable、Vector 和 Stack 中使用。
ListIterator繼承Iterator
/**
* An ListIterator is used to sequence over a List of objects. ListIterator can
* move backwards or forwards through the list.
*/
public interface ListIterator<E> extends Iterator<E> {}
以上便是集合的主要內容,我們下面的系列都是按照上面的內容進行詳細的分析和總結,歡迎持續關注~
關於作者
專注於 Android 開發多年,喜歡寫 blog 記錄總結學習經驗,blog 同步更新於本人的公衆號,歡迎大家關注,一起交流學習~
- GitHub:
https://github.com/crazyandcoder - 公衆號:碼農的小世界