【Java】【集合】

來源:互聯網
上載者:User

標籤:cut   購物車   ack   hashmap   color   nbsp   board   dha   sample   

【1. 】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 
  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 
  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的數組實現的功能很像.

 

 【2. 】

 

【Java】【集合】

聯繫我們

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