有序的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取键值对时,是按照你放入的顺序来取的。

[java]  view plain  copy
 print ?
  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]  view plain  copy
 print ?
  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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值