標籤:
Map
|--Hashtable:底層是雜湊表資料結構,不可以存入null鍵null值。該集合是線程同步的。jdk1.0.效率低。
|--HashMap:底層是雜湊表資料結構,允許使用 null 值和 null 鍵,該集合是不同步的。將hashtable替代,jdk1.2.效率高。
|--TreeMap:底層是二叉樹資料結構。線程不同步。可以用於給map集合中的鍵進行排序。
和Set很像。
其實大家,Set底層就是使用了Map集合。
/*map集合的兩種取出方式:1,Set<k> keySet:將map中所有的鍵存入到Set集合。因為set具備迭代器。 所有可以迭代方式取出所有的鍵,在根據get方法。擷取每一個鍵對應的值。 Map集合的取出原理:將map集合轉成set集合。在通過迭代器取出。2,Set<Map.Entry<k,v>> entrySet:將map集合中的映射關係存入到了set集合中, 而這個關係的資料類型就是:Map.Entry Entry其實就是Map中的一個static內部介面。 為什麼要定義在內部呢? 因為只有有了Map集合,有了索引值對,才會有索引值的映射關係。 關係屬於Map集合中的一個內部事物。 而且該事物在直接存取Map集合中的元素。*/import java.util.*;class MapDemo2 { public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("02","zhangsan2"); map.put("03","zhangsan3"); map.put("01","zhangsan1"); map.put("04","zhangsan4"); //將Map集合中的映射關係取出。存入到Set集合中。 Set<Map.Entry<String,String>> entrySet = map.entrySet(); Iterator<Map.Entry<String,String>> it = entrySet.iterator(); while(it.hasNext()) { Map.Entry<String,String> me = it.next(); String key = me.getKey(); String value = me.getValue(); System.out.println(key+":"+value); } /* //先擷取map集合的所有鍵的Set集合,keySet(); Set<String> keySet = map.keySet(); //有了Set集合。就可以擷取其迭代器。 Iterator<String> it = keySet.iterator(); while(it.hasNext()) { String key = it.next(); //有了鍵可以通過map集合的get方法擷取其對應的值。 String value = map.get(key); System.out.println("key:"+key+",value:"+value); } */ }}/*Map.Entry 其實Entry也是一個介面,它是Map介面中的一個內部介面。interface Map{ public static interface Entry { public abstract Object getKey(); public abstract Object getValue(); }}class HashMap implements Map{ class Hahs implements Map.Entry { public Object getKey(){} public Object getValue(){} } }*/
java中Map集合的理解