【转】LinkedHashMap实现由插入集合的顺序输出

(转载声明:

  作者: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: mmz06@163.com 
  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. }  
原文地址:https://www.cnblogs.com/qneverever/p/4448172.html