【Java集合】集合框架Collection和Map

【Java集合】集合框架Collection和Map

    在面試的時候,一般從java基礎開始問,而Java集合框架被問的概率一定是位於前列。剛開始,被問到java集合框架,List,Set,Map脫口而出。而其實真正學習過java集合的,答案肯定不一樣。

    一. 集合由來

    在程式設計中,常常需要集中存放多個資料。從傳統意義上講,陣列是我們的一個很好的選擇,前提是我們事先已經明確知道我們將要儲存的物件的數量。一旦在陣列初始化時指定了這個陣列長度,這個陣列長度就是不可變的,如果我們需要儲存一個可以動態增長的資料(在編譯時無法確定具體的數量),java的集合類就是一個很好的設計方案了。

    二. 集合基本概念

    集合類主要負責儲存、盛裝其他資料,因此集合類也被稱為容器類。所有的Java集合類都位於java.util包下,後來為了處理多執行緒環境下的併發安全問題,java5還在java.util.concurrent包下提供了一些多執行緒支援的集合類。

    值得注意的幾點:

①、集合只能存放物件。比如你存一個 int 型資料 1放入集合中,其實它是自動轉換成 Integer 類後存入的,Java中每一種基本型別都有對應的引用型別。
②、集合存放的是多個物件的引用,物件本身還是放在堆記憶體中。
③、集合可以存放不同型別,不限數量的資料型別。

    三. Java集合框架

    Java的集合類主要由兩個介面派生而出:Collection和Map,Collection和Map是Java集合框架的根介面。

這裡寫圖片描述

    圖中,ArrayList,HashSet,LinkedList,TreeSet是我們經常會有用到的已實現的集合類。

    Map實現類用於儲存具有對映關係的資料。Map儲存的每項資料都是key-value對,也就是由key和value兩個值組成。Map裡的key是不可重複的,key使用者標識集合裡的每項資料。
這裡寫圖片描述

    圖中,HashMap,TreeMap是我們經常會用到的集合類。

    Colleciton和Map介面中的方法,是我們在操作list,set,map等具體實現類中會經常應用到的,所以,參照著jdk文件,做個總結。

    四. Collection介面

    Collection介面是Set,Queue,List的父介面。Collection介面中定義了多種方法可供其子類進行實現,以實現資料操作。

  1. int size()

    Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
    返回集合中元素的個數,若個數大於最大值,則返回最大值。

  2. boolean isEmpty()

    Returns true if this collection contains no elements.
    返回true,若當前集合不包含任何元素。

  3. boolean contains(Object o)

    Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).
    返回true,若當前集合中包含某個具體的元素。 當且僅當當前集合至少包含一個元素,判斷的具體元素為null,同樣返回true.

    Throws:
        ClassCastException – if the type of the specified element is incompatible with this collection (optional)
        型別轉換異常,若具體的元素型別與當前集合型別不相容
        NullPointerException – if the specified element is null and this collection does not permit null elements (optional)
        空指標異常,若具體的元素為null,且當前集合不允許包含null元素

  4. Iterator iterator()

    Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).
    返回此集合中元素的迭代器。對於返回元素的順序沒有保證(除非這個集合是提供保證的某個類的例項)。

  5. boolean add(E e)

    Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

    Collections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.

    If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.

    確保此集合包含指定元素(可選操作)。如果此集合作為呼叫的結果而更改,則返回true。(如果此集合不允許重複並已包含指定的元素,則返回false)。

    支援此操作的集合可能會限制哪些元素可以新增到此集合中。特別是,一些集合將拒絕新增空元素,而其他集合將對可能新增的元素的型別施加限制。集合類應該在文件中明確地規定對哪些元素可以新增的任何限制。

    如果集合拒絕任何特定元素的新增,除了它已經包含元素之外,它必須丟擲異常(而不是返回false)。這保留了集合在呼叫返回後始終包含指定元素的不變數。

    Throws:
        UnsupportedOperationException – if the add operation is not supported by this collection
        不支援操作異常,若當前集合不支援增加元素操作
        ClassCastException – if the class of the specified element prevents it from being added to this collection
        型別轉換異常,若具體的元素型別不允許新增至當前集合
        NullPointerException – if the specified element is null and this collection does not permit null elements (optional)
        空指標異常,若具體的元素為null,且當前集合不允許包含null元素
        IllegalArgumentException – if some property of the element prevents it from being added to this collection
        若當前元素的某些屬性不允許新增至當前集合
        IllegalStateException – if the element cannot be added at this time due to insertion restrictions
        由於插入限制,該元素不能新增至當前集合

  6. boolean remove(Object o)

    Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).
    從集合中移除指定元素的單個例項.

    Throws:
        ClassCastException – if the type of the specified element is incompatible with this collection (optional)
        型別轉換異常,若具體的元素型別與當前集合型別不相容
        NullPointerException – if the specified element is null and this collection does not permit null elements (optional)
        空指標異常,若具體的元素為null,且當前集合不允許包含null元素
        UnsupportedOperationException – if the add operation is not supported by this collection
        不支援操作異常,若當前集合不支援增加元素操作

  7. boolean containsAll(Collection c)

    Returns true if this collection contains all of the elements in the specified collection.
    返回true,若當前集合包含指定集合中的所有元素.

  8. boolean addAll(Collection c)

    Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)
    將指定集合中的所有元素新增到此集合中

  9. boolean removeAll(Collection c)

    Removes all of this collection’s elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.
    移除包含在指定集合中的所有集合元素。此呼叫返回後,此集合將不包含與指定集合相同的元素。

  10. boolean retainAll(Collection c)

    Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.
    只保留包含在指定集合中的集合中的元素。換句話說,從該集合中移除其未包含在指定集合中的所有元素。

    五. Map介面

Map是鍵值對的形式,和Collections中也有著相同的方法,這裡不再重複總結了,下面主要是一些對鍵值對操作的方法。

  1. boolean containsKey(Object key)

    Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)
    若當前map中包含某個具體的key,則返回true.

  2. boolean containsValue(Object value)

    Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface.
    若當前map中包含某個或多個key對應的值,則返回true.

  3. V get(Object key)

    Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
    獲取map中某個Key對應的值,若當前map中不包含此key,則返回null

  4. V put(K key,V value)

    Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)
    新增某鍵值對至當前map中,若當前map中已包含新增的key,則key的值將會被新值替代。

  5. V remove(Object key)

    Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)
    從當前map中移除某key.

  6. void putAll(Map m)

    Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
    將某個map中所有元素新增至當前map中,此操作等同於使用put()方法迴圈新增。