java map集合的知識

來源:互聯網
上載者:User

標籤:判斷   key   syn   示範   刪除   code   shm   tag   能力   

  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. }  

java map集合的知識

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.