java集合最后一站之Map,給自己的總結畫個句號。。。
Map用于保存具有映射關系的數據。
1.HashMap和Hashtable實現類
HashMap和Hashtable都是Map接口的典型實現類,它們之間的關系完全類似于Arraylist和Vecctor的關系。
區別:
Hashtable是線程安全的,HashMap是線程不安全的,所以HashMap比Hashtable的性能高一點。
Hashtable不允許使用null作為key和value;但HashMap可以使用null作為key或value。
HashMap和Hashtable判斷兩個value相等的標準:只要兩個對象通過equals方法比較返回true即可。
import java.util.*;class A{int count;public A(int count){this.count = count;}public boolean equals(Object obj){if (obj == this){return true;}if (obj != null && obj.getClass() == A.class){A a = (A)obj;if (this.count == a.count){return true;}}return false;}public int hashCode(){return this.count;}}class B{PRivate String name;public B(String name) {super();this.name = name;}public B() {super();}public boolean equals(Object obj){return true;}}public class TestHashMap{public static void main(String[] args) {HashMap hm = new HashMap();hm.put(new A(60000) , "Struts2");hm.put(new A(87563) , "J2EE");hm.put(new A(1232) , "Xiao");System.out.println(hm);//{A@ea60=Struts2, A@4d0=Xiao, A@1560b=J2EE}//只要兩個對象通過equals比較返回true,Hashtable就認為它們是相等的value。//因為B對象equals總是返回true,所以下面是trueSystem.out.println(hm.containsValue(new B("測試")));//true//只要兩個A對象的count屬性相等,它們通過equals比較返回true,且hashCode相等//Hashtable即認為它們是相同的key,所以下面輸出true。System.out.println(hm.containsKey(new A(87563)));//true//下面語句可以刪除最后一個key-value對hm.remove(new A(1232));for (Object key : hm.keySet()){System.out.print(key + "---->");System.out.print(hm.get(key) + "/n");}}}
輸出結果:
{A@ea60=Struts2, A@4d0=Xiao, A@1560b=J2EE}truetrueA@ea60---->Struts2A@1560b---->J2EE
2.LinkedHashMap實現類
LinkedHashMap實現類使用鏈表來維護key-value的次序,可以記住鍵值對的插入順序。
import java.util.*;public class TestLinkedHashMap{public static void main(String[] args) {LinkedHashMap scores = new LinkedHashMap();scores.put("語文" , 80);scores.put("數學" , 76);scores.put("英文" , 76);//遍歷scores里的所有的key-value對for (Object key : scores.keySet()){System.out.print(key + "------>");System.out.print(scores.get(key) + "/n");}}}
輸出結果:
語文------>80數學------>76英文------>76
3.SoetedMap接口和TreeMap實現類
TreeMap存儲key-value鍵值對時,需要根據key對節點進行排序。TreeMap可以保證所有的key-value對處于有序狀態。也有兩種排序方式:
1) 自然排序:TreeMap的所有key必須實現Comparable接口,而且所有的key應該是同一個類的對象,否則拋出ClassCastException異常。
2) 定制排序:創建TreeMap時,傳入一個Comparator對象,該對象負責對TreeMap中的所有key進行排序。不需要Map的key實現Comparable接口。
import java.util.*;//R類,重寫了equals方法,如果count屬性相等返回true//重寫了compareTo(Object obj)方法,如果count屬性相等返回0;class R implements Comparable{int count;public R(int count){this.count = count;}public String toString(){return "R(count屬性:" + count + ")";}public boolean equals(Object obj){if (this == obj){return true;}if (obj != null && obj.getClass() == R.class){R r = (R)obj;if (r.count == this.count){return true;}}return false;}public int compareTo(Object obj){R r = (R)obj;if (this.count > r.count){return 1;}else if (this.count == r.count){return 0;}else{return -1;}}}public class TestTreeMap{public static void main(String[] args) {TreeMap tm = new TreeMap();tm.put(new R(3) , "J2EE");tm.put(new R(-5) , "Struts2");tm.put(new R(9) , "ROR");System.out.println(tm);//{R(count屬性:-5)=Struts2, R(count屬性:3)=J2EE, R(count屬性:9)=ROR}//返回該TreeMap的第一個Entry對象System.out.println(tm.firstEntry());//返回該TreeMap的最后一個key值System.out.println(tm.lastKey());//返回該TreeMap的比new R(2)大的最小key值。System.out.println(tm.higherKey(new R(2)));//返回該TreeMap的比new R(2)小的最大的key-value對。System.out.println(tm.lowerEntry(new R(2)));//返回該TreeMap的子TreeMapSystem.out.println(tm.subMap(new R(-1) , new R(4)));}}
輸出結果:
{R(count屬性:-5)=Struts2, R(count屬性:3)=J2EE, R(count屬性:9)=ROR}R(count屬性:-5)=Struts2R(count屬性:9)R(count屬性:3)R(count屬性:-5)=Struts2{R(count屬性:3)=J2EE}
4.各Map實現類的性能分析:
1. HashMap與Hashtable的效率大體相同,它們的實現機制幾乎一樣,HashMap線程不安全,Hashtable線程安全,所以HashMap快一點。
2. TreeMap中所有的key-value對處于有序狀態,所以TreeMap比HashMap,Hashtable要慢(尤其是插入、刪除),因為TreeMap底層采用紅黑樹來管理key-value對。
3. LinkedHashMap使用鏈表維護鍵值對,所以比HahMap慢一點。
對于一般的·應用場景,推薦使用HashMap。
轉發請注明出處:http://www.49028c.com/jycboy/p/javamap.html
新聞熱點
疑難解答