java map集合的知识

  1. /**  
  2.  * Map用于存储键值对,不允许键重复,值可以重复。  
  3.  * (1)HashMap是一个最常用的Map,它根据键的hashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。  
  4.  * HashMap最多只允许一条记录的键为null,允许多条记录的值为null。  
  5.  * HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap,可能会导致数据的不一致。  
  6.  * 如果需要同步,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。  
  7.  * (2)Hashtable与HashMap类似,不同的是:它不允许记录的键或者值为空;  
  8.  * 它支持线程的同步,即任一时刻只有一个线程能写Hashtable,然而,这也导致了Hashtable在写入时会比较慢。  
  9.  * (3)LinkedHashMap保存了记录的插入顺序,在用Iteraor遍历LinkedHashMap时,先得到的记录肯定是先插入的。  
  10.  * 在遍历的时候会比HashMap慢。  
  11.  * (4)TreeMap能够把它保存的记录根据键排序,默认是按升序排序,也可以指定排序的比较器。当用Iteraor遍历TreeMap时,  
  12.  * 得到的记录是排过序的。  
  13.  */  
  14. /**  
  15.  * 演示各个Map的实现类  
  16.  */  
  17. public class TestMap {  
  18.       
  19.     /**  
  20.      * 初始化一个Map  
  21.      * @param map  
  22.      */  
  23.     public static void init(Map map){  
  24.         if (map != null){  
  25.             String key = null;  
  26.             for (int i=5; i>0; i--){  
  27.                 key = new Integer(i).toString() + ".0";  
  28.                 map.put(key, key.toString());  
  29.                 //Map中的键是不重复的,如果插入两个键值一样的记录,  
  30.                 //那么后插入的记录会覆盖先插入的记录  
  31.                 map.put(key, key.toString() + "0");         }  
  32.         }  
  33.     }  
  34.     /**  
  35.      * 输出一个Map  
  36.      * @param map  
  37.      */  
  38.     public static void output(Map map){  
  39.         if (map != null){  
  40.             Object key = null;  
  41.             Object value = null;  
  42.             //使用迭代器遍历Map的键,根据键取值  
  43.             Iterator it = map.keySet().iterator();  
  44.             while (it.hasNext()){  
  45.                 key = it.next();  
  46.                 value = map.get(key);  
  47.                 System.out.println("key: " + key + "; value: " + value );  
  48.             }  
  49.             //或者使用迭代器遍历Map的记录Map.Entry  
  50.             Map.Entry entry = null;  
  51.             it = map.entrySet().iterator();  
  52.             while (it.hasNext()){  
  53.                 //一个Map.Entry代表一条记录  
  54.                 entry = (Map.Entry)it.next();  
  55.                 //通过entry可以获得记录的键和值  
  56.                 System.out.println("key: " + entry.getKey() + "; value: " + entry.getValue());  
  57.             }  
  58.         }  
  59.     }  
  60.     /**  
  61.      * 判断map是否包含某个键  
  62.      * @param map  
  63.      * @param key  
  64.      * @return  
  65.      */  
  66.     public static boolean containsKey(Map map, Object key){  
  67.         if (map != null){  
  68.             return map.containsKey(key);  
  69.         }  
  70.         return false;  
  71.     }  
  72.     /**  
  73.      * 判断map是否包含某个值  
  74.      * @param map  
  75.      * @param value  
  76.      * @return  
  77.      */  
  78.     public static boolean containsValue(Map map, Object value){  
  79.         if (map != null){  
  80.             return map.containsValue(value);  
  81.         }  
  82.         return false;  
  83.     }  
  84.     /**  
  85.      * 演示HashMap  
  86.      */  
  87.     public static void testHashMap(){  
  88.         Map myMap = new HashMap();  
  89.         init(myMap);  
  90.         //HashMap的键可以为null  
  91.         myMap.put(null,"ddd");  
  92.         //HashMap的值可以为null  
  93.         myMap.put("aaa", null);  
  94.         output(myMap);  
  95.     }  
  96.     /**  
  97.      * 演示Hashtable  
  98.      */  
  99.     public static void testHashtable(){  
  100.         Map myMap = new Hashtable();  
  101.         init(myMap);  
  102.         //Hashtable的键不能为null  
  103.         //myMap.put(null,"ddd");  
  104.         //Hashtable的值不能为null  
  105.         //myMap.put("aaa", null);  
  106.         output(myMap);  
  107.     }  
  108.     /**  
  109.      * 演示LinkedHashMap  
  110.      */  
  111.     public static void testLinkedHashMap(){  
  112.         Map myMap = new LinkedHashMap();  
  113.         init(myMap);  
  114.         //LinkedHashMap的键可以为null  
  115.         myMap.put(null,"ddd");  
  116.         //LinkedHashMap的值可以为null  
  117.         myMap.put("aaa", null);  
  118.         output(myMap);  
  119.     }  
  120.     /**  
  121.      * 演示TreeMap  
  122.      */  
  123.     public static void testTreeMap(){  
  124.         Map myMap = new TreeMap();  
  125.         init(myMap);  
  126.         //TreeMap的键不能为null  
  127.         //myMap.put(null,"ddd");  
  128.         //TreeMap的值不能为null  
  129.         //myMap.put("aaa", null);  
  130.         output(myMap);  
  131.     }  
  132.   
  133.     public static void main(String[] args) {  
  134.         System.out.println("采用HashMap");  
  135.         TestMap.testHashMap();  
  136.         System.out.println("采用Hashtable");  
  137.         TestMap.testHashtable();  
  138.         System.out.println("采用LinkedHashMap");  
  139.         TestMap.testLinkedHashMap();  
  140.         System.out.println("采用TreeMap");  
  141.         TestMap.testTreeMap();  
  142.           
  143.         Map myMap = new HashMap();  
  144.         TestMap.init(myMap);  
  145.         System.out.println("新初始化一个Map: myMap");  
  146.         TestMap.output(myMap);  
  147.         //清空Map  
  148.         myMap.clear();  
  149.         System.out.println("将myMap clear后,myMap空了么?  " + myMap.isEmpty());  
  150.         TestMap.output(myMap);  
  151.         myMap.put("aaa", "aaaa");  
  152.         myMap.put("bbb", "bbbb");  
  153.         //判断Map是否包含某键或者某值  
  154.         System.out.println("myMap包含键aaa?  "+ TestMap.containsKey(myMap, "aaa"));  
  155.         System.out.println("myMap包含值aaaa?  "+ TestMap.containsValue(myMap, "aaaa"));  
  156.         //根据键删除Map中的记录  
  157.         myMap.remove("aaa");  
  158.         System.out.println("删除键aaa后,myMap包含键aaa?  "+ TestMap.containsKey(myMap, "aaa"));  
  159.         //获取Map的记录数  
  160.         System.out.println("myMap包含的记录数:  " + myMap.size());  
  161.     }  
  162.       
  163. }  
原文地址:https://www.cnblogs.com/wangyage/p/6930595.html