List、Set、Map的源碼初級分析,setmap
Set、Map、List都是利用利用大量的泛型的。
List和Set都是繼承collection介面的。而Map是沒有繼承任何的介面的。
public interface List<E> extends Collection<E> public interface Set<E> extends Collection<E> {public interface Map<K,V> {
Map源碼描述:一個有鍵值對的對象。一個map不能包含重複的鍵值對。每一個鍵最多隻能匹配一個值。這個借口可以取代Dictionary類。Dictionary類是一個抽象類別而不是一個介面。
原文:
* An object that mapskeys to values. A map cannot containduplicate keys;
* each key can map to at most one value.
*
* <p>This interface takes the place of the<tt>Dictionary</tt> class, which
* was a totally abstract class rather than aninterface.
List源碼描述:一個有序的Collection(作為sequence也是被廣為人知),使用List介面的使用者能夠精確控制在插入的元素的位置。使用者可以通過下標來訪問和查詢裡面的元素。List和Set不同,能夠插入重複的資料。
原文:
An ordered collection(also known as a<i>sequence</i>). The user ofthis
* interface has precise control over where inthe list each element is
* inserted. The user can access elements by their integer index (position in
* the list), and search for elements in thelist.<p>
*
* Unlike sets, lists typically allow duplicateelements.
Set:一個不會包含相同元素的Collection。更加準確的講,Set是不會包含一對像能夠e1.equals(e2)的這樣元素,還有至多隻有一個空元素。正如它名字所指出的,這個介面類比數學上的集合的概念。
原文:
A collection thatcontains no duplicate elements. Moreformally, sets contain no pair ofelements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface modelsthe mathematical setabstraction.
再看下他們實作類別ArrayList和HashMap的源碼,會發現ArrayList裡面
封裝的是一個Object數組,HashMap是封裝一個內部類的數組
ArrayList的構造方法部分:
public ArrayList(int initialCapacity) { initialCapacity);……… this.elementData = new Object[initialCapacity]; }
它執行個體化的時候,預設數組大小為10:
public ArrayList() { this(10);}
HashMap裡面不是利用Object進行封裝,而是有一個內部類Entry:
構造方法裡面有一句:
table = new Entry[capacity];static final int DEFAULT_INITIAL_CAPACITY = 16;
預設容量是16