List、Set、Map的源碼初級分析,setmap

來源:互聯網
上載者:User

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





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.