zl程序教程

您现在的位置是:首页 >  其他

当前栏目

HashMap、LinkedHashMap和TreeMap对比详解编程语言

编程语言 详解 对比 HashMap LinkedHashMap TreeMap
2023-06-13 09:20:46 时间

共同点:

HashMap,LinkedHashMap,TreeMap都属于Map;Map 主要用于存储键(key)值(value)对,根据键得到值,因此键不允许键重复,但允许值重复。  

不同点:

1.HashMap里面存入的键值对在取出的时候是随机的,也是我们最常用的一个Map.它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。在Map 中插入、删除和定位元素,HashMap 是最好的选择。  

2.TreeMap取出来的是排序后的键值对。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。  

3. LinkedHashMap 是HashMap的一个子类,如果需要输出的顺序和输入的相同,那么用LinkedHashMap可以实现.  (应用场景:购物车等需要顺序的)

代码实例:

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()); 

} 

返回结果:

------无序(随机输出)------ 

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 

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16046.html

cjava