java資料結構

來源:互聯網
上載者:User

標籤:

一、List介面,有序的Collection介面,可以精確地控制每一個元素插入的位置,同意有同樣的元素

1.鏈表,LinkedList實現了List介面,同意null元素,提供了get()、remove()、insert()方法,沒有同步方法

public void add() { LinkedList List = new LinkedList(); List.add("link1");List.add("link2");List.add("link3");Iterator it = List.iterator(); while (it.hasNext()) {System.out.println(it.next());}it.remove(); Iterator it1 = List.iterator(); for (int i = 0; i < List.size(); i++) {System.out.println(it1.next());}}

2.數組列表,ArrayList,能夠動態變化容量的數組,非同步的

數組列表中存放的是Object類型,因此在數組列表中存放的物件類型,以其原型的父類取代,提取當中的元素時要進行類型轉換

public static void main(String[] args) { ArrayList al=new ArrayList(); al.add("name"); al.add("value"); al.add("number"); for(int i=0;i<al.size();i++) { System.out.println(al.get(i)); }}
二、Set介面,不包括反覆元素的Collection介面
1.散列集,HashSet,實現了Set介面,非線性同步
與鏈表和數組列表差點兒類似,但在資料處理時,比使用鏈表進行資料處理花費時間更短,處理大資料時通常使用散列集

public static void main(String[] args) { long time=0; HashSet hs=new HashSet(); ArrayList al=new ArrayList(); long starttime=System.currentTimeMillis(); for(int i=0;i<10000;i++) { hs.add(new Integer(i)); } System.out.println(System.currentTimeMillis()-starttime); for(int i=0;i<10000;i++) { al.add(new Integer(i)); } System.out.println(System.currentTimeMillis()-starttime); }
2.樹集,TreeSet,實現了Set介面,實現了排序功能,該集合中的元素預設按升序排列元素
使用樹集資料結構的對象,須要實現Comparable介面,樹集輸出的資料都是經過排序的


三、Map介面,沒有繼承Collection介面,其提供key到value的映射,Map中不能包括同樣的key,每一個key僅僅能映射一個value。
1.散列表類,HashTable,繼承了Map介面,非空(non-null)的對象都可作為key或value,是同步的
加入資料:put(key,value);
取出資料:get(key);
特點:無序的,因此能夠高速尋找特定的元素
public static void TableTest(){Hashtable ht = new Hashtable();ht.put("key1", "value1");ht.put("key2", "value2");String value1=(String)ht.get("key2");System.out.println(value1);}
建構函式:Hashtable(),初始容量11,負載因子0.75,即散列表中已經有75%的位置被放滿,則進行再散列。
負載因子越高(越接近1),則記憶體使用量率越高,元素的尋找時間越長,反之,則記憶體浪費越多,尋找時間越短
key的對象將通過計算散函數確定與之相應的value位置 ,因此key的對象須要實現hashCode()和equals()方法。
兩個對象同樣,則它們的hashCode必須同樣;兩個對象不同,則它們的hashCode不一定不同。
衝突:不同對象的hashCode同樣,則稱為衝突,衝突將導致操作散列表的時間開銷增大。因此要同一時候覆寫equals()和hashCode()方法
2.散列映射類,HashMap,與HashTable類似,不是HashMap是非同步的,且同意null
public static void Maptest(){Map<string string=""> map=new HashMap<string string="">();map.put("key1", "value1");map.put("key2", "value2");map.put("key3", "value3");for(Map.Entry<string string=""> entry:map.entrySet()){System.out.println(entry.getKey());System.out.println(entry.getValue());}String value1=(String)map.get("key1");System.out.println(value1);}</string></string></string>
3.樹映像,TreeMap,輸出的資料依照keyword排序了

java資料結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.