有序的map LinkedHashMap

HashMap是无序的,HashMap在put的时候是根据key的hashcode进行hash然后放入对应的地方。所以在按照一定顺序put进HashMap中,然后遍历出HashMap的顺序跟put的顺序不同(除非在put的时候key已经按照hashcode排序号了,这种几率非常小)
单纯的HashMap是无法实现排序的,这的排序是指,我们将键值对按照一定的顺序put进HashMap里,然后在进行取键值对的操作的时候,是按照put进去的顺序把键值对取出来的。

JAVA在JDK1.4以后提供了LinkedHashMap来帮助我们实现了有序的HashMap!

LinkedHashMap取键值对时,是按照你放入的顺序来取的。

 1 [java] view plain copy print?
 2 import java.util.HashMap;    
 3 import java.util.Iterator;    
 4 import java.util.LinkedHashMap;    
 5 import java.util.Map;    
 6 import java.util.Map.Entry;    
 7 /**  
 8  * @author  TEANA E-mail: mmz06@163.com  
 9  * @version 创建时间:2011-1-21 下午02:23:07  
10  * @DO      LinkedHashMap与HashMap     
11  */    
12 public class LinkedMap    
13 {    
14     public static void main(String[] args)    
15     {    
16         //LinkedHashMap 有序    
17         Map maps = new LinkedHashMap();    
18         maps.put("1", "张三");    
19         maps.put("2", "李四");    
20         maps.put("3", "王五");    
21         maps.put("4", "赵六");    
22         System.out.println("LinkedHashMap(有序):");    
23         Iterator it = maps.entrySet().iterator();    
24         while(it.hasNext())    
25         {    
26             Map.Entry entity = (Entry) it.next();    
27             System.out.println("[ key = " + entity.getKey() +     
28                     ", value = " + entity.getValue() + " ]");    
29         }    
30         //HashMap 无序    
31         Map map = new HashMap();    
32         map.put("1", "张三");    
33         map.put("2", "李四");    
34         map.put("3", "王五");    
35         map.put("4", "赵六");    
36         it = null;    
37         System.out.println("HashMap(无序):");    
38         it = map.entrySet().iterator();    
39         while(it.hasNext())    
40         {    
41             Map.Entry entity = (Entry) it.next();    
42             System.out.println("[ key = " + entity.getKey() +     
43                     ", value = " + entity.getValue() + " ]");    
44         }    
45     }    
46 }  
View Code

2017-05-1817:56:34

执行结果如下:
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可以实现.

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

输出结果: 
------无序(随机输出------
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 

此篇转载自 http://blog.csdn.net/u012889434/article/details/48055679 么么哒!!

原文地址:https://www.cnblogs.com/little-fly/p/6874700.html