HashMap,LinkedHashMap,TreeMap对比

来源:互联网 发布:充电风扇淘宝网 编辑:程序博客网 时间:2024/05/16 17:45

共同点:

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. package com.alibaba.sample.petstore.web.store.module.screen;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Iterator;  
  5. import java.util.LinkedHashMap;  
  6. import java.util.Map;  
  7. import java.util.Map.Entry;  
  8. import java.util.TreeMap;  
  9.   
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import org.springframework.beans.factory.annotation.Autowired;  
  13.   
  14. public class ViewCart {  
  15.     @Autowired  
  16.     private HttpServletResponse response;  
  17.   
  18.     public void execute() throws Exception {  
  19.         this.useHashMap();      
  20.         this.useTreeMap();  
  21.         this.useLikedHashMap();   
  22.     }  
  23.       
  24.     public void useHashMap() throws Exception {      
  25.         response.getWriter().println("------无序(随机输出)------");  
  26.         Map<String, String> map = new HashMap<String, String>();      
  27.         map.put("1""Level 1");      
  28.         map.put("2""Level 2");      
  29.         map.put("3""Level 3");      
  30.         map.put("a""Level a");      
  31.         map.put("b""Level b");      
  32.         map.put("c""Level c");  
  33.         Iterator<Entry<String, String>> it = map.entrySet().iterator();      
  34.         while (it.hasNext()) {       
  35.             Entry<String, String> e = it.next();       
  36.             response.getWriter().println("Key: " + e.getKey() + ";   Value: "       + e.getValue());      
  37.         }  
  38.     }  
  39.       
  40.     // 有序(默认排序,不能指定)   
  41.     public void useTreeMap() throws Exception {      
  42.         response.getWriter().println("------有序(但是按默认顺充,不能指定)------");      
  43.         Map<String, String> map = new TreeMap<String, String>();      
  44.         map.put("1""Level 1");      
  45.         map.put("2""Level 2");      
  46.         map.put("3""Level 3");      
  47.         map.put("a""Level a");      
  48.         map.put("b""Level b");      
  49.         map.put("c""Level c");      
  50.         Iterator<Entry<String, String>> it = map.entrySet().iterator();      
  51.         while (it.hasNext()) {       
  52.             Entry<String, String> e = it.next();       
  53.             response.getWriter().println("Key: " + e.getKey() + ";   Value: "       + e.getValue());      
  54.         }  
  55.     }  
  56.       
  57.     public void useLikedHashMap() throws Exception {      
  58.         response.getWriter().println("------有序(根据输入的顺序输出)------");      
  59.         Map<String, String> map = new LinkedHashMap<String, String>();      
  60.         map.put("1""Level 1");      
  61.         map.put("2""Level 2");      
  62.         map.put("3""Level 3");      
  63.         map.put("a""Level a");      
  64.         map.put("b""Level b");      
  65.         map.put("c""Level c");  
  66.         Iterator<Entry<String, String>> it = map.entrySet().iterator();      
  67.         while (it.hasNext()) {       
  68.             Entry<String, String> e = it.next();       
  69.             response.getWriter().println("Key: " + e.getKey() + ";   Value: "       + e.getValue());      
  70.         }  
  71.     }  
  72. }  

返回结果:
[html] view plain copy print?
  1. ------无序(随机输出)------  
  2. Key: 3;   Value: Level 3  
  3. Key: 2;   Value: Level 2  
  4. Key: 1;   Value: Level 1  
  5. Key: b;   Value: Level b  
  6. Key: c;   Value: Level c  
  7. Key: a;   Value: Level a  
  8. ------有序(但是按默认顺充,不能指定)------  
  9. Key: 1;   Value: Level 1  
  10. Key: 2;   Value: Level 2  
  11. Key: 3;   Value: Level 3  
  12. Key: a;   Value: Level a  
  13. Key: b;   Value: Level b  
  14. Key: c;   Value: Level c  
  15. ------有序(根据输入的顺序输出)------  
  16. Key: 1;   Value: Level 1  
  17. Key: 2;   Value: Level 2  
  18. Key: 3;   Value: Level 3  
  19. Key: a;   Value: Level a  
  20. Key: b;   Value: Level b  
  21. Key: c;   Value: Level c  

4.小结:php的数组就是用hashmap实现的,所以学过php对hashmap就很容易理解,linkedhashmap跟php的数组实现的功能很像.
0 0