標籤: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
- package com.alibaba.sample.petstore.web.store.module.screen;
-
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.TreeMap;
-
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.beans.factory.annotation.Autowired;
-
- public class ViewCart {
- @Autowired
- private HttpServletResponse response;
-
- public void execute() throws Exception {
- this.useHashMap();
- this.useTreeMap();
- this.useLikedHashMap();
- }
-
- public void useHashMap() throws Exception {
- response.getWriter().println("------無序(隨機輸出)------");
- Map<String, String> map = new HashMap<String, String>();
- map.put("1", "Level 1");
- map.put("2", "Level 2");
- map.put("3", "Level 3");
- map.put("a", "Level a");
- map.put("b", "Level b");
- map.put("c", "Level c");
- Iterator<Entry<String, String>> it = map.entrySet().iterator();
- while (it.hasNext()) {
- Entry<String, String> e = it.next();
- response.getWriter().println("Key: " + e.getKey() + "; Value: " + e.getValue());
- }
- }
-
- // 有序(預設排序,不能指定)
- public void useTreeMap() throws Exception {
- response.getWriter().println("------有序(但是按預設順充,不能指定)------");
- Map<String, String> map = new TreeMap<String, String>();
- map.put("1", "Level 1");
- map.put("2", "Level 2");
- map.put("3", "Level 3");
- map.put("a", "Level a");
- map.put("b", "Level b");
- map.put("c", "Level c");
- Iterator<Entry<String, String>> it = map.entrySet().iterator();
- while (it.hasNext()) {
- Entry<String, String> e = it.next();
- response.getWriter().println("Key: " + e.getKey() + "; Value: " + e.getValue());
- }
- }
-
- public void useLikedHashMap() throws Exception {
- response.getWriter().println("------有序(根據輸入的順序輸出)------");
- Map<String, String> map = new LinkedHashMap<String, String>();
- map.put("1", "Level 1");
- map.put("2", "Level 2");
- map.put("3", "Level 3");
- map.put("a", "Level a");
- map.put("b", "Level b");
- map.put("c", "Level c");
- Iterator<Entry<String, String>> it = map.entrySet().iterator();
- while (it.hasNext()) {
- Entry<String, String> e = it.next();
- response.getWriter().println("Key: " + e.getKey() + "; Value: " + e.getValue());
- }
- }
- }
返回結果:
[html] view plain copy
- ------無序(隨機輸出)------
- Key: 3; Value: Level 3
- Key: 2; Value: Level 2
- Key: 1; Value: Level 1
- Key: b; Value: Level b
- Key: c; Value: Level c
- Key: a; Value: Level a
- ------有序(但是按預設順充,不能指定)------
- Key: 1; Value: Level 1
- Key: 2; Value: Level 2
- Key: 3; Value: Level 3
- Key: a; Value: Level a
- Key: b; Value: Level b
- Key: c; Value: Level c
- ------有序(根據輸入的順序輸出)------
- Key: 1; Value: Level 1
- Key: 2; Value: Level 2
- Key: 3; Value: Level 3
- Key: a; Value: Level a
- Key: b; Value: Level b
- Key: c; Value: Level c
4.小結:php的數組就是用hashmap實現的,所以學過php對hashmap就很容易理解,linkedhashmap跟php的數組實現的功能很像.
【2. 】
【Java】【集合】