1.ArrayListjava
是一個可改變大小的數組.當更多的元素加入到ArrayList中時,其大小將會動態地增加.內部的元素能夠直接經過get與set方法進行訪問,由於ArrayList本質上就是一個數組.不一樣步(就是線程不安全)數組
ArrayList是一個動態數組,也是咱們最經常使用的集合。它容許任何符合規則的元素插入甚至包括null。每個ArrayList都有一個初始容量(10),該容量表明瞭數組的大小。隨着容器中的元素不斷增長,容器的大小也會隨着增長。在每次向容器中增長元素的同時都會進行容量檢查,當快溢出時,就會進行擴容操做。因此若是咱們明確所插入元素的多少,最好指定一個初始容量值,避免過多的進行擴容操做而浪費時間、效率。安全
size、isEmpty、get、set、iterator 和 listIterator 操做都以固定時間運行。add 操做以分攤的固定時間運行,也就是說,添加 n 個元素須要 O(n) 時間(因爲要考慮到擴容,因此這不僅是添加元素會帶來分攤固定時間開銷那樣簡單)。ide
ArrayList擅長於隨機訪問。同時ArrayList是非同步的。函數
ArrayList :性能
1 package test; 2 //package com.algorithm.java.Niit2.Learning; 3 4 //import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator; 5 6 import java.util.ArrayList; 7 import java.util.ListIterator; 8 9 public class ArrayListDemo { 10 public static void main(String[] args){ 11 ArrayList<String> obj=new ArrayList<String>(); 12 String sobj1=new String("Element 1"); 13 String sobj2=new String("Element 2"); 14 String sobj3=new String("Element 3"); 15 String sobj4=new String("Element 4"); 16 17 System.out.println("Size of ArrayList is :"+obj.size()); 18 obj.add(sobj1); 19 obj.add(sobj2); 20 obj.add(sobj3); 21 obj.add(sobj4); 22 obj.add(sobj1); 23 24 System.out.println("\nArrayList after adding the objects:"+obj); 25 System.out.println("\nSize of ArrayList after adding object "+obj.size()); 26 27 obj.remove(2); 28 obj.remove(sobj4); 29 30 System.out.println("\nArrayList after removing the objects"+obj); 31 System.out.println("Size of ArrayList after removing objects:"+obj.size()); 32 33 System.out.println("\nThe final ArrayList :"); 34 ListIterator i= (ListIterator) obj.listIterator(); 35 while(i.hasNext()){ 36 System.out.println(i.next()); 37 38 } 39 40 } 41 42 }
2.LinkedListspa
是一個雙鏈表,在添加和刪除元素時具備比ArrayList更好的性能.但在get與set方面弱於ArrayList.不一樣步(就是線程不安全)線程
一樣實現List接口的LinkedList與ArrayList不一樣,ArrayList是一個動態數組,而LinkedList是一個雙向鏈表。因此它除了有ArrayList的基本操做方法外還額外提供了get,remove,insert方法在LinkedList的首部或尾部。code
因爲實現的方式不一樣,LinkedList不能隨機訪問,它全部的操做都是要按照雙重鏈表的須要執行。在列表中索引的操做將從開頭或結尾遍歷列表(從靠近指定索引的一端)。這樣作的好處就是能夠經過較低的代價在List中進行插入和刪除操做。xml
與ArrayList同樣,LinkedList也是非同步的。若是多個線程同時訪問一個List,則必須本身實現訪問同步。一種解決方法是在建立List時構造一個同步的List:
List list = Collections.synchronizedList(new LinkedList(...));
LinkedList:
1 package test; 2 //package com.algorithm.java.Niit2.Learning; 3 4 //import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator; 5 6 import java.util.ArrayList; 7 import java.util.LinkedList; 8 import java.util.ListIterator; 9 10 public class LinkedListDemo { 11 public static void main(String[] args){ 12 LinkedList<String> obj=new LinkedList<String>(); 13 String sobj1=new String("Element 1"); 14 String sobj2=new String("Element 2"); 15 String sobj3=new String("Element 3"); 16 String sobj4=new String("Element 4"); 17 18 System.out.println("Size of ArrayList is :"+obj.size()); 19 obj.add(sobj1); 20 obj.add(sobj2); 21 obj.add(sobj3); 22 obj.add(sobj4); 23 obj.add(sobj1); 24 25 System.out.println("\nArrayList after adding the objects:"+obj); 26 System.out.println("\nSize of ArrayList after adding object "+obj.size()); 27 28 obj.remove(2); 29 obj.remove(sobj4); 30 31 System.out.println("\nArrayList after removing the objects"+obj); 32 System.out.println("Size of ArrayList after removing objects:"+obj.size()); 33 34 System.out.println("\nThe final ArrayList :"); 35 ListIterator i= (ListIterator) obj.listIterator(); 36 while(i.hasNext()){ 37 System.out.println(i.next()); 38 39 } 40 41 } 42 43 }
3.Vector
和ArrayList相似,但屬於強同步類。若是你的程序自己是線程安全的(thread-safe,沒有在多個線程之間共享同一個集合/對象),那麼使用ArrayList是更好的選擇。.同步(線程安全)
有句話叫越安全,效率就越低。
Vector:
1 package test; 2 //package com.algorithm.java.Niit2.Learning; 3 4 //import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator; 5 6 import java.util.ArrayList; 7 import java.util.LinkedList; 8 import java.util.ListIterator; 9 import java.util.Vector; 10 11 public class LinkedListDemo { 12 public static void main(String[] args){ 13 Vector<String> obj=new Vector<String>(); 14 String sobj1=new String("Element 1"); 15 String sobj2=new String("Element 2"); 16 String sobj3=new String("Element 3"); 17 String sobj4=new String("Element 4"); 18 19 System.out.println("Size of ArrayList is :"+obj.size()); 20 obj.add(sobj1); 21 obj.add(sobj2); 22 obj.add(sobj3); 23 obj.add(sobj4); 24 obj.add(sobj1); 25 26 System.out.println("\nArrayList after adding the objects:"+obj); 27 System.out.println("\nSize of ArrayList after adding object "+obj.size()); 28 29 obj.remove(2); 30 obj.remove(sobj4); 31 32 System.out.println("\nArrayList after removing the objects"+obj); 33 System.out.println("Size of ArrayList after removing objects:"+obj.size()); 34 35 System.out.println("\nThe final ArrayList :"); 36 ListIterator i= (ListIterator) obj.listIterator(); 37 while(i.hasNext()){ 38 System.out.println(i.next()); 39 40 } 41 42 } 43 44 }
Map接口有三個比較重要的實現類,分別是HashMap、TreeMap和HashTable:
1.HashMap:哈希表:衝突處理爲單鏈表;無序
2.TreeMap:二叉樹:有序
3.HashTable:哈希表:衝突處理爲單鏈表
*************Hashtable的方法是同步的,HashMap的方法不是同步的
Hashtable是線程安全的,HashMap不是線程安全的。
HashMap效率較高,Hashtable效率較低。
=========================================================================================
HashMap:
注:基於哈希表的 Map 接口的實現。此實現提供全部可選的映射操做,並容許使用 null 值和 null 鍵。(除了非同步和容許使用 null 以外,HashMap 類與 Hashtable 大體相同。)此類不保證映射的順序,特別是它不保證該順序恆久不變。 此實現假定哈希函數將元素適當地分佈在各桶之間,可爲基本操做(get 和 put)提供穩定的性能。迭代 collection 視圖所需的時間與 HashMap 實例的「容量」(桶的數量)及其大小(鍵-值映射關係數)成比例。因此,若是迭代性能很重要,則不要將初始容量設置得過高(或將加載因子設置得過低)。