HashMap Hashtable LinkedHashMap TreeMap

来源:互联网 发布:linux 查找文件 子目录 编辑:程序博客网 时间:2024/05/22 16:59
  1. import java.util.HashMap;  
  2. import java.util.Hashtable;  
  3. import java.util.LinkedHashMap;  
  4. import java.util.Map;  
  5. import java.util.TreeMap;  
  6. import java.util.Map.Entry;  
  7.   
  8.   
  9. public class testMap {  
  10.   
  11.     /** 
  12.      * @param args 
  13.      */  
  14.     public static void main(String[] args) {  
  15.         Map<Integer,String> map = new HashMap<Integer,String>();  
  16.         map.put(6"apple");  
  17.         map.put(3"banana");  
  18.         map.put(2,"pear");  
  19.           
  20.         Map<Integer,String> map1 = new LinkedHashMap<Integer,String>();  
  21.         map1.put(6"apple");  
  22.         map1.put(3"banana");  
  23.         map1.put(2,"pear");  
  24.           
  25.         Map<Integer,String> map2 = new TreeMap<Integer,String>();  
  26.         map2.put(6"apple");  
  27.         map2.put(3"banana");  
  28.         map2.put(2,"pear");  
  29.           
  30.         Hashtable<Integer,String> tab = new Hashtable<Integer,String>();  
  31.         tab.put(6"apple");  
  32.         tab.put(3"banana");  
  33.         tab.put(2,"pear");  
  34.           
  35.         System.out.println("HashMap");  
  36.         //HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable  
  37.         for(Entry<Integer,String> e:map.entrySet()){  
  38.             System.out.println(e.getKey()+":"+e.getValue());  
  39.         }  
  40.         System.out.println("LinkedHashMap");  
  41.         //LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>  
  42.         for(Entry<Integer,String> e:map1.entrySet()){  
  43.             System.out.println(e.getKey()+":"+e.getValue());  
  44.         }  
  45.         System.out.println("TreeMap");  
  46.         //TreeMap<K,V> extends AbstractMap<K,V> implements SortedMap<K,V>, Cloneable, java.io.Serializable  
  47.         for(Entry<Integer,String> e:map2.entrySet()){  
  48.             System.out.println(e.getKey()+":"+e.getValue());  
  49.         }  
  50.         System.out.println("Hashtable");  
  51.         //Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, java.io.Serializable   
  52.         for(Entry<Integer,String> e:tab.entrySet()){  
  53.             System.out.println(e.getKey()+":"+e.getValue());  
  54.         }  
  55.         /**输出结果: 
  56.          *  HashMap 
  57.             2:pear 
  58.             6:apple 
  59.             3:banana 
  60.              
  61.             LinkedHashMap 
  62.             6:apple 
  63.             3:banana 
  64.             2:pear 
  65.              
  66.             TreeMap 
  67.             2:pear 
  68.             3:banana 
  69.             6:apple 
  70.              
  71.             Hashtable 
  72.             6:apple 
  73.             3:banana 
  74.             2:pear 
  75.          *  
  76.          * */  
  77.           
  78.     }  
  79.   
  80. }  
  81.  
  82. java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMap Hashtable LinkedHashMap 和TreeMap. 
  83. Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复  
  84. (1) Hashmap 是一个最常用的Map,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度,遍历时,取得数据的顺序是完全随机的。 HashMap最多只允许一条记录的键为Null;允许多条记录的值为 Null;HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap;可能会导致数据的不一致。如果需要同步,可以用 Collections的synchronizedMap方法使HashMap具有同步的能力,或者使用ConcurrentHashMap。
  85. (2) Hashtable与 HashMap类似,它继承自Dictionary类,不同的是:它不允许记录的键或者值为空;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了 Hashtable在写入时会比较慢。
  86. (3) LinkedHashMap 是HashMap的一个子类保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的.也可以在构造时用带参数,按照应用次数排序。在遍历的时候会比HashMap慢,不过有种情况例外,当HashMap容量很大,实际数据较少时,遍历起来可能会比 LinkedHashMap慢,因为LinkedHashMap的遍历速度只和实际数据有关,和容量无关,而HashMap的遍历速度和他的容量有关。
  87. (4) TreeMap实现SortMap接口,能够把它保存的记录根据键排序,默认是按键值的升序排序,也可以指定排序的比较器,当用Iterator 遍历TreeMap时,得到的记录是排过序的。
  88.  
  89. 一般情况下,我们用的最多的是HashMap,在Map 中插入、删除和定位元素,HashMap 是最好的选择。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。如果需要输出的顺序和输入的相同,那么用LinkedHashMap 可以实现,它还可以按读取顺序来排列.  
0 0
原创粉丝点击