什么是List,也就是一個有序集合(序列)。
1.List接口
List集合代表一個有序集合,集合中每個元素都有其對應的順序索引。List集合允許使用重復元素,可以通過索引來訪問指定位置的集合元素。
List接口繼承于Collection接口,它可以定義一個允許重復的有序集合。因為List中的元素是有序的,所以我們可以通過使用索引(元素在List中的位置,類似于數組下標)來訪問List中的元素,這類似于Java的數組。
List接口為Collection直接接口。List所代表的是有序的Collection,即它用某種特定的插入順序來維護元素順序。用戶可以對列表中每個元素的插入位置進行精確地控制,同時可以根據元素的整數索引(在列表中的位置)訪問元素,并搜索列表中的元素。實現List接口的集合主要有:ArrayList、LinkedList、Vector、Stack。
(1)ArrayList
????? ArrayList是一個動態數組,也是我們最常用的集合。它允許任何符合規則的元素插入甚至包括null。每一個ArrayList都有一個初始容量(10),該容量代表了數組的大小。隨著容器中的元素不斷增加,容器的大小也會隨著增加。在每次向容器中增加元素的同時都會進行容量檢查,當快溢出時,就會進行擴容操作。所以如果我們明確所插入元素的多少,最好指定一個初始容量值,避免過多的進行擴容操作而浪費時間、效率。
????? size、isEmpty、get、set、iterator 和 listIterator 操作都以固定時間運行。add 操作以分攤的固定時間運行,也就是說,添加 n 個元素需要 O(n) 時間(由于要考慮到擴容,所以這不只是添加元素會帶來分攤固定時間開銷那樣簡單)。
??????ArrayList擅長于隨機訪問。同時ArrayList是非同步的。
(2)LinkedList
????? 同樣實現List接口的LinkedList與ArrayList不同,ArrayList是一個動態數組,而LinkedList是一個雙向鏈表。所以它除了有ArrayList的基本操作方法外還額外提供了get,remove,insert方法在LinkedList的首部或尾部。
????? 由于實現的方式不同,LinkedList不能隨機訪問,它所有的操作都是要按照雙重鏈表的需要執行。在列表中索引的操作將從開頭或結尾遍歷列表(從靠近指定索引的一端)。這樣做的好處就是可以通過較低的代價在List中進行插入和刪除操作。
????? 與ArrayList一樣,LinkedList也是非同步的。如果多個線程同時訪問一個List,則必須自己實現訪問同步。一種解決方法是在創建List時構造一個同步的List:?
List list = Collections.synchronizedList(new LinkedList(...));
(3)Vector
????? 與ArrayList相似,但是Vector是同步的。所以說Vector是線程安全的動態數組。它的操作與ArrayList幾乎一樣。
(4)Stack
???? Stack繼承自Vector,實現一個后進先出的堆棧。Stack提供5個額外的方法使得Vector得以被當作堆棧使用。基本的push和pop 方法,還有peek方法得到棧頂的元素,empty方法測試堆棧是否為空,search方法檢測一個元素在堆棧中的位置。Stack剛創建后是空棧。
以下來自JDK1.9 關于List
Public interface List<E> extends Collection<E>
Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements?e1
?and?e2
?such that?e1.equals(e2)
, and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.
與集合不同,列表通常允許重復的元素。更正式的說,列表通常允許成對的元素e1和e2,比如e1.equals(e2),如果它們允許零元素,它們通常允許多個空元素。有人可能希望實現一個禁止重復的列表,在用戶試圖插入時拋出運行時異常,這不是不可想象的,但我們希望這種用法非常少見。
The?List
?interface places additional stipulations, beyond those specified in the?Collection
?interface, on the contracts of the?iterator
,?add
,?remove
,?equals
, andhashCode
?methods. Declarations for other inherited methods are also included here for convenience.
List接口將額外的規定放置在迭代器的契約、添加、移除、equals和hashcode方法上,超出了集合接口中指定的條款。為了方便起見,這里還包括了其他繼承方法的聲明。
The?List
?interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the?LinkedList
?class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.
List接口提供了四種方法來定位(索引)對列表元素的訪問。列表(如Java數組)是基于0的。請注意,這些操作可能會與某些實現的索引值成比例(例如,LinkedList類)。因此,如果調用者不知道實現,那么遍歷列表中的元素通常更可取。
The?List
?interface provides a special iterator, called a?ListIterator
, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the?Iterator
?interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.
List接口提供了一個特殊的迭代器,稱為ListIterator,除了Iterator接口提供的常規操作之外,它還允許元素插入和替換,以及雙向訪問。提供了一種方法來獲得列表迭代器,它從列表中指定的位置開始。
The?List
?interface provides two methods to search for a specified object. From a performance standpoint, these methods should be used with caution. In many implementations they will perform costly linear searches.
List接口提供了兩種搜索指定對象的方法。從性能的角度來看,這些方法應該謹慎使用。在許多實現中,它們將執行代價高昂的線性搜索。
The?List
?interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.
List接口提供了兩種方法,可以有效地在列表中的任意一點插入和刪除多個元素。
Note: While it is permissible for lists to contain themselves as elements, extreme caution is advised: the?equals
?and?hashCode
?methods are no longer well defined on such a list.
注意:雖然列表可以將自己作為元素來包含,但是要特別注意:在這樣的列表中,equals和hashCode方法不再有很好的定義。
Some list implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically?NullPointerException
?or?ClassCastException
. Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the list may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as "optional" in the specification for this interface。