【轉】LinkedHashMap實現由插入集合的順序輸出

來源:互聯網
上載者:User

標籤:

(轉載聲明:

  kingdelee

  地址:http://kingdelee.iteye.com/blog/1582135

  )

HashMap是無序的,HashMap在put的時候是根據key的hashcode進行hash然後放入對應的地方。所以在按照一定順序put進HashMap中,然後遍曆出HashMap的順序跟put的順序不同(除非在put的時候key已經按照hashcode排序號了,這種幾率非常小) 
單純的HashMap是無法實現排序的,這的排序是指,我們將索引值對按照一定的順序put進HashMap裡,然後在進行取索引值對的操作的時候,是按照put進去的順序把索引值對取出來的。 
JAVA在JDK1.4以後提供了LinkedHashMap來協助我們實現了有序的HashMap! 
LinkedHashMap取索引值對時,是按照你放入的順序來取的。 

Java代碼  
  1. import java.util.HashMap;  
  2. import java.util.Iterator;  
  3. import java.util.LinkedHashMap;  
  4. import java.util.Map;  
  5. import java.util.Map.Entry;  
  6. /** 
  7.  * @author  TEANA E-mail: [email protected] 
  8.  * @version 建立時間:2011-1-21 下午02:23:07 
  9.  * @DO      LinkedHashMap與HashMap    
  10.  */  
  11. public class LinkedMap  
  12. {  
  13.     public static void main(String[] args)  
  14.     {  
  15.         //LinkedHashMap 有序  
  16.         Map maps = new LinkedHashMap();  
  17.         maps.put("1", "張三");  
  18.         maps.put("2", "李四");  
  19.         maps.put("3", "王五");  
  20.         maps.put("4", "趙六");  
  21.         System.out.println("LinkedHashMap(有序):");  
  22.         Iterator it = maps.entrySet().iterator();  
  23.         while(it.hasNext())  
  24.         {  
  25.             Map.Entry entity = (Entry) it.next();  
  26.             System.out.println("[ key = " + entity.getKey() +   
  27.                     ", value = " + entity.getValue() + " ]");  
  28.         }  
  29.         //HashMap 無序  
  30.         Map map = new HashMap();  
  31.         map.put("1", "張三");  
  32.         map.put("2", "李四");  
  33.         map.put("3", "王五");  
  34.         map.put("4", "趙六");  
  35.         it = null;  
  36.         System.out.println("HashMap(無序):");  
  37.         it = map.entrySet().iterator();  
  38.         while(it.hasNext())  
  39.         {  
  40.             Map.Entry entity = (Entry) it.next();  
  41.             System.out.println("[ key = " + entity.getKey() +   
  42.                     ", value = " + entity.getValue() + " ]");  
  43.         }  
  44.     }  
  45. }  


執行結果如下: 
LinkedHashMap(有序): 
[ key = 1, value = 張三 ] 
[ key = 2, value = 李四 ] 
[ key = 3, value = 王五 ] 
[ key = 4, value = 趙六 ] 
HashMap(無序): 
[ key = 3, value = 王五 ] 
[ key = 2, value = 李四 ] 
[ key = 1, value = 張三 ] 
[ key = 4, value = 趙六 ] 

HashMap,LinkedHashMap,TreeMap應用簡介 
共同點: 
HashMap,LinkedHashMap,TreeMap都屬於Map;Map 主要用於儲存鍵(key)值(value)對,根據鍵得到值,因此鍵不允許鍵重複,但允許值重複。 
不同點: 
1.HashMap裡面存入的索引值對在取出的時候是隨機的,也是我們最常用的一個Map.它根據鍵的HashCode值儲存資料,根據鍵可以直接擷取它的值,具有很快的訪問速度。在Map 中插入、刪除和定位元素,HashMap 是最好的選擇。 
2.TreeMap取出來的是排序後的索引值對。但如果您要按自然順序或自訂順序遍曆鍵,那麼TreeMap會更好。 
3. LinkedHashMap 是HashMap的一個子類,如果需要輸出的順序和輸入的相同,那麼用LinkedHashMap可以實現. 

Java代碼  
  1. import java.util.HashMap;  
  2. import java.util.Iterator;  
  3. import java.util.LinkedHashMap;  
  4. import java.util.Map;  
  5. import java.util.TreeMap;   
  6. public class MapAppTest {  
  7. /** 
  8. * @Create on Nov 9, 2009 by lrm 
  9. */  
  10. public static void main(String[] args) {  
  11.    // TODO Auto-generated method stub  
  12.    MapAppTest.noOrder();  
  13.    MapAppTest.hasOrder();  
  14.    MapAppTest.likedHashMap();  
  15. }   
  16. public static void noOrder() {  
  17.    System.out.println("------無序(隨機輸出------");  
  18.    Map map = new HashMap();  
  19.    map.put("1", "Level 1");  
  20.    map.put("2", "Level 2");  
  21.    map.put("3", "Level 3");  
  22.    map.put("4", "Level 4");  
  23.    map.put("F", "Level F");  
  24.    map.put("Q", "Level Q");  
  25.    Iterator it = map.entrySet().iterator();  
  26.    while (it.hasNext()) {  
  27.     Map.Entry e = (Map.Entry) it.next();  
  28.     System.out.println("Key: " + e.getKey() + ";   Value: "  
  29.       + e.getValue());  
  30.    }  
  31. }   
  32. // 有序(預設排序,不能指定)  
  33. public static void hasOrder() {  
  34.    System.out.println("------有序(但是按預設順充,不能指定)------");  
  35.    Map map = new TreeMap();  
  36.    map.put("F", "Level F");  
  37.    map.put("7", "Level 1");  
  38.    map.put("8", "Level 2");  
  39.    map.put("4", "Level 3");  
  40.    map.put("4", "Level 4");  
  41.    map.put("Q", "Level Q");  
  42.    map.put("E", "Level E");  
  43.    Iterator it = map.entrySet().iterator();  
  44.    while (it.hasNext()) {  
  45.     Map.Entry e = (Map.Entry) it.next();  
  46.     System.out.println("Key: " + e.getKey() + ";   Value: "  
  47.       + e.getValue());  
  48.    }  
  49. }   
  50. public static void likedHashMap() {  
  51.    System.out.println("------有序(根據輸入的順序輸出)------");  
  52.    Map map = new LinkedHashMap();  
  53.    map.put("F", "Level F");  
  54.    map.put("7", "Level 1");  
  55.    map.put("8", "Level 2");  
  56.    map.put("4", "Level 3");  
  57.    map.put("4", "Level 4");  
  58.    map.put("Q", "Level Q");  
  59.    map.put("E", "Level E");  
  60.    Iterator it = map.entrySet().iterator();  
  61.    while (it.hasNext()) {  
  62.     Map.Entry e = (Map.Entry) it.next();  
  63.     System.out.println("Key: " + e.getKey() + ";   Value: "  
  64.       + e.getValue());  
  65.    }  
  66. }   
  67. }   



輸出結果: 
------無序(隨機輸出------ 
Key: 3;   Value: Level 3 
Key: F;   Value: Level F 
Key: 2;   Value: Level 2 
Key: 4;   Value: Level 4 
Key: Q;   Value: Level Q 
Key: 1;   Value: Level 1 
------有序(但是按預設順充,不能指定)------ 
Key: 4;   Value: Level 4 
Key: 7;   Value: Level 1 
Key: 8;   Value: Level 2 
Key: E;   Value: Level E 
Key: F;   Value: Level F 
Key: Q;   Value: Level Q 
------有序(根據輸入的順序輸出)------ 
Key: F;   Value: Level F 
Key: 7;   Value: Level 1 
Key: 8;   Value: Level 2 
Key: 4;   Value: Level 4 
Key: Q;   Value: Level Q 
Key: E;   Value: Level E 

Java代碼  
    1. package cn.itcast.p1.map.demo;  
    2.   
    3. import java.io.File;  
    4. import java.util.HashMap;  
    5. import java.util.Iterator;  
    6. import java.util.LinkedHashMap;  
    7. import java.util.Map;  
    8.   
    9. public class LinkedHashMapDemo {  
    10.   
    11.     /** 
    12.      * @param args 
    13.      */  
    14.     public static void main(String[] args) {  
    15.           
    16.         File f= null;  
    17.         HashMap<Integer,String> hm = new LinkedHashMap<Integer,String>();  
    18.           
    19.         hm.put(7, "zhouqi");  
    20.         hm.put(3, "zhangsan");  
    21.         hm.put(1, "qianyi");  
    22.         hm.put(5, "wangwu");  
    23.           
    24.         Iterator<Map.Entry<Integer,String>> it = hm.entrySet().iterator();  
    25.           
    26.         while(it.hasNext()){  
    27.             Map.Entry<Integer,String> me = it.next();  
    28.               
    29.             Integer key = me.getKey();  
    30.             String value = me.getValue();  
    31.               
    32.             System.out.println(key+":"+value);  
    33.         }  
    34.     }  
    35.   
    36. }  

【轉】LinkedHashMap實現由插入集合的順序輸出

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.