HashMap、Hashtable、LinkedHashMap和TreeMap
Posted on 2009-05-31 00:13 啥都寫點(diǎn) 閱讀(1659) 評(píng)論(0) 編輯 收藏 所屬分類: J2SE關(guān)鍵技術(shù):
- HashMap是一個(gè)最常用的Map,它根據(jù)鍵的hashCode值存儲(chǔ)數(shù)據(jù),根據(jù)鍵可以直接獲取它的值,具有很快的訪問速度。HashMap最多只允許一條記錄的鍵為NULL,允許多條記錄的值為NULL。HashMap不支持線程同步,即任一時(shí)刻可以有多個(gè)線程同時(shí)寫HashMap,可能會(huì)導(dǎo)致數(shù)據(jù)的不一致性。如果需要同步,可以用Collections的synchronizedMap方法使HashMap具有同步的能力。
- Hashtable與HashMap類似,不同的是:它不允許記錄的鍵或者值為空;它支持線程的同步,即任一時(shí)刻只有一個(gè)線程能寫Hashtable,因此也導(dǎo)致了Hashtable在寫入時(shí)會(huì)比較慢。
- LinkedHashMap保存了記錄的插入順序,在用Iterator遍歷LinkedHashMap時(shí),先得到的記錄肯定是先插入的。在遍歷的時(shí)候會(huì)比HashMap慢
- TreeMap能夠把它保存的記錄根據(jù)鍵排序,默認(rèn)是按升序排序,也可以指定排序的比較器。當(dāng)用Iterator遍歷TreeMap時(shí),得到的記錄是排過序的。
package book.arrayset;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* 演示各個(gè)Map的實(shí)現(xiàn)類
*/
public class TestMap {
/**
* 初始化一個(gè)Map
* @param map
*/
public static void init(Map map){
if (map != null){
String key = null;
for (int i=5; i>0; i--){
key = new Integer(i).toString() + ".0";
map.put(key, key.toString());
//Map中的鍵是不重復(fù)的,如果插入兩個(gè)鍵值一樣的記錄,
//那么后插入的記錄會(huì)覆蓋先插入的記錄
map.put(key, key.toString() + "0"); }
}
}
/**
* 輸出一個(gè)Map
* @param map
*/
public static void output(Map map){
if (map != null){
Object key = null;
Object value = null;
//使用迭代器遍歷Map的鍵,根據(jù)鍵取值
Iterator it = map.keySet().iterator();
while (it.hasNext()){
key = it.next();
value = map.get(key);
System.out.println("key: " + key + "; value: " + value );
}
//或者使用迭代器遍歷Map的記錄Map.Entry
Map.Entry entry = null;
it = map.entrySet().iterator();
while (it.hasNext()){
//一個(gè)Map.Entry代表一條記錄
entry = (Map.Entry)it.next();
//通過entry可以獲得記錄的鍵和值
//System.out.println("key: " + entry.getKey() + "; value: " + entry.getValue());
}
}
}
/**
* 判斷map是否包含某個(gè)鍵
* @param map
* @param key
* @return
*/
public static boolean containsKey(Map map, Object key){
if (map != null){
return map.containsKey(key);
}
return false;
}
/**
* 判斷map是否包含某個(gè)值
* @param map
* @param value
* @return
*/
public static boolean containsValue(Map map, Object value){
if (map != null){
return map.containsValue(value);
}
return false;
}
/**
* 演示HashMap
*/
public static void testHashMap(){
Map myMap = new HashMap();
init(myMap);
//HashMap的鍵可以為null
myMap.put(null,"ddd");
//HashMap的值可以為null
myMap.put("aaa", null);
output(myMap);
}
/**
* 演示Hashtable
*/
public static void testHashtable(){
Map myMap = new Hashtable();
init(myMap);
//Hashtable的鍵不能為null
//myMap.put(null,"ddd");
//Hashtable的值不能為null
//myMap.put("aaa", null);
output(myMap);
}
/**
* 演示LinkedHashMap
*/
public static void testLinkedHashMap(){
Map myMap = new LinkedHashMap();
init(myMap);
//LinkedHashMap的鍵可以為null
myMap.put(null,"ddd");
//LinkedHashMap的值可以為null
myMap.put("aaa", null);
output(myMap);
}
/**
* 演示TreeMap
*/
public static void testTreeMap(){
Map myMap = new TreeMap();
init(myMap);
//TreeMap的鍵不能為null
//myMap.put(null,"ddd");
//TreeMap的值不能為null
//myMap.put("aaa", null);
output(myMap);
}
public static void main(String[] args) {
System.out.println("采用HashMap");
TestMap.testHashMap();
System.out.println("采用Hashtable");
TestMap.testHashtable();
System.out.println("采用LinkedHashMap");
TestMap.testLinkedHashMap();
System.out.println("采用TreeMap");
TestMap.testTreeMap();
Map myMap = new HashMap();
TestMap.init(myMap);
System.out.println("新初始化一個(gè)Map: myMap");
TestMap.output(myMap);
//清空Map
myMap.clear();
System.out.println("將myMap clear后,myMap空了么? " + myMap.isEmpty());
TestMap.output(myMap);
myMap.put("aaa", "aaaa");
myMap.put("bbb", "bbbb");
//判斷Map是否包含某鍵或者某值
System.out.println("myMap包含鍵aaa? "+ TestMap.containsKey(myMap, "aaa"));
System.out.println("myMap包含值aaaa? "+ TestMap.containsValue(myMap, "aaaa"));
//根據(jù)鍵刪除Map中的記錄
myMap.remove("aaa");
System.out.println("刪除鍵aaa后,myMap包含鍵aaa? "+ TestMap.containsKey(myMap, "aaa"));
//獲取Map的記錄數(shù)
System.out.println("myMap包含的記錄數(shù): " + myMap.size());
}
/**
* Map用于存儲(chǔ)鍵值對(duì),不允許鍵重復(fù),值可以重復(fù)。
* (1)HashMap是一個(gè)最常用的Map,它根據(jù)鍵的hashCode值存儲(chǔ)數(shù)據(jù),根據(jù)鍵可以直接獲取它的值,具有很快的訪問速度。
* HashMap最多只允許一條記錄的鍵為null,允許多條記錄的值為null。
* HashMap不支持線程的同步,即任一時(shí)刻可以有多個(gè)線程同時(shí)寫HashMap,可能會(huì)導(dǎo)致數(shù)據(jù)的不一致。
* 如果需要同步,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。
* (2)Hashtable與HashMap類似,不同的是:它不允許記錄的鍵或者值為空;
* 它支持線程的同步,即任一時(shí)刻只有一個(gè)線程能寫Hashtable,然而,這也導(dǎo)致了Hashtable在寫入時(shí)會(huì)比較慢。
* (3)LinkedHashMap保存了記錄的插入順序,在用Iteraor遍歷LinkedHashMap時(shí),先得到的記錄肯定是先插入的。
* 在遍歷的時(shí)候會(huì)比HashMap慢。
* (4)TreeMap能夠把它保存的記錄根據(jù)鍵排序,默認(rèn)是按升序排序,也可以指定排序的比較器。當(dāng)用Iteraor遍歷TreeMap時(shí),
* 得到的記錄是排過序的。
*/
}
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* 演示各個(gè)Map的實(shí)現(xiàn)類
*/
public class TestMap {
/**
* 初始化一個(gè)Map
* @param map
*/
public static void init(Map map){
if (map != null){
String key = null;
for (int i=5; i>0; i--){
key = new Integer(i).toString() + ".0";
map.put(key, key.toString());
//Map中的鍵是不重復(fù)的,如果插入兩個(gè)鍵值一樣的記錄,
//那么后插入的記錄會(huì)覆蓋先插入的記錄
map.put(key, key.toString() + "0"); }
}
}
/**
* 輸出一個(gè)Map
* @param map
*/
public static void output(Map map){
if (map != null){
Object key = null;
Object value = null;
//使用迭代器遍歷Map的鍵,根據(jù)鍵取值
Iterator it = map.keySet().iterator();
while (it.hasNext()){
key = it.next();
value = map.get(key);
System.out.println("key: " + key + "; value: " + value );
}
//或者使用迭代器遍歷Map的記錄Map.Entry
Map.Entry entry = null;
it = map.entrySet().iterator();
while (it.hasNext()){
//一個(gè)Map.Entry代表一條記錄
entry = (Map.Entry)it.next();
//通過entry可以獲得記錄的鍵和值
//System.out.println("key: " + entry.getKey() + "; value: " + entry.getValue());
}
}
}
/**
* 判斷map是否包含某個(gè)鍵
* @param map
* @param key
* @return
*/
public static boolean containsKey(Map map, Object key){
if (map != null){
return map.containsKey(key);
}
return false;
}
/**
* 判斷map是否包含某個(gè)值
* @param map
* @param value
* @return
*/
public static boolean containsValue(Map map, Object value){
if (map != null){
return map.containsValue(value);
}
return false;
}
/**
* 演示HashMap
*/
public static void testHashMap(){
Map myMap = new HashMap();
init(myMap);
//HashMap的鍵可以為null
myMap.put(null,"ddd");
//HashMap的值可以為null
myMap.put("aaa", null);
output(myMap);
}
/**
* 演示Hashtable
*/
public static void testHashtable(){
Map myMap = new Hashtable();
init(myMap);
//Hashtable的鍵不能為null
//myMap.put(null,"ddd");
//Hashtable的值不能為null
//myMap.put("aaa", null);
output(myMap);
}
/**
* 演示LinkedHashMap
*/
public static void testLinkedHashMap(){
Map myMap = new LinkedHashMap();
init(myMap);
//LinkedHashMap的鍵可以為null
myMap.put(null,"ddd");
//LinkedHashMap的值可以為null
myMap.put("aaa", null);
output(myMap);
}
/**
* 演示TreeMap
*/
public static void testTreeMap(){
Map myMap = new TreeMap();
init(myMap);
//TreeMap的鍵不能為null
//myMap.put(null,"ddd");
//TreeMap的值不能為null
//myMap.put("aaa", null);
output(myMap);
}
public static void main(String[] args) {
System.out.println("采用HashMap");
TestMap.testHashMap();
System.out.println("采用Hashtable");
TestMap.testHashtable();
System.out.println("采用LinkedHashMap");
TestMap.testLinkedHashMap();
System.out.println("采用TreeMap");
TestMap.testTreeMap();
Map myMap = new HashMap();
TestMap.init(myMap);
System.out.println("新初始化一個(gè)Map: myMap");
TestMap.output(myMap);
//清空Map
myMap.clear();
System.out.println("將myMap clear后,myMap空了么? " + myMap.isEmpty());
TestMap.output(myMap);
myMap.put("aaa", "aaaa");
myMap.put("bbb", "bbbb");
//判斷Map是否包含某鍵或者某值
System.out.println("myMap包含鍵aaa? "+ TestMap.containsKey(myMap, "aaa"));
System.out.println("myMap包含值aaaa? "+ TestMap.containsValue(myMap, "aaaa"));
//根據(jù)鍵刪除Map中的記錄
myMap.remove("aaa");
System.out.println("刪除鍵aaa后,myMap包含鍵aaa? "+ TestMap.containsKey(myMap, "aaa"));
//獲取Map的記錄數(shù)
System.out.println("myMap包含的記錄數(shù): " + myMap.size());
}
/**
* Map用于存儲(chǔ)鍵值對(duì),不允許鍵重復(fù),值可以重復(fù)。
* (1)HashMap是一個(gè)最常用的Map,它根據(jù)鍵的hashCode值存儲(chǔ)數(shù)據(jù),根據(jù)鍵可以直接獲取它的值,具有很快的訪問速度。
* HashMap最多只允許一條記錄的鍵為null,允許多條記錄的值為null。
* HashMap不支持線程的同步,即任一時(shí)刻可以有多個(gè)線程同時(shí)寫HashMap,可能會(huì)導(dǎo)致數(shù)據(jù)的不一致。
* 如果需要同步,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。
* (2)Hashtable與HashMap類似,不同的是:它不允許記錄的鍵或者值為空;
* 它支持線程的同步,即任一時(shí)刻只有一個(gè)線程能寫Hashtable,然而,這也導(dǎo)致了Hashtable在寫入時(shí)會(huì)比較慢。
* (3)LinkedHashMap保存了記錄的插入順序,在用Iteraor遍歷LinkedHashMap時(shí),先得到的記錄肯定是先插入的。
* 在遍歷的時(shí)候會(huì)比HashMap慢。
* (4)TreeMap能夠把它保存的記錄根據(jù)鍵排序,默認(rèn)是按升序排序,也可以指定排序的比較器。當(dāng)用Iteraor遍歷TreeMap時(shí),
* 得到的記錄是排過序的。
*/
}
-- 學(xué)海無涯