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相关文章
- mybaties日常开发总结详解编程语言
- Hibernate_day03讲义_使用Hibernate完成多对多的关系映射并操作详解编程语言
- [javaEE] Servlet的手动配置详解编程语言
- JVM运行期数据域详解编程语言
- Java数据结构和算法(五)——队列详解编程语言
- java 时间戳与日期字符串相互转换详解编程语言
- jvm调优、垃圾回收机制等详解编程语言
- EasyUI中combobox的使用方法详解编程语言
- easyui和ie8兼容性问题分析详解编程语言
- springboot整合docker部署(两种构建Docker镜像方式)详解编程语言
- 理解ThreadPoolExecutor线程池的corePoolSize、maximumPoolSize和poolSize详解编程语言
- 汉诺塔-递归实现详解编程语言
- Python3 根据m3u8下载视频,批量下载ts文件并且合并详解编程语言
- springBoot项目不重新上传jar包,增量升级步骤详解编程语言
- Java数据持久层框架 MyBatis之API学习八(Java API详解)编程语言
- abstract详解编程语言
- 练手系列(2) 最长有效括号的长度详解编程语言
- SAP最常用的业务流程事务代码详解编程语言
- ini_set详解编程语言
- Java多线程13:读写锁和两种同步方式的对比详解编程语言
- 自己实现一个Native方法的调用详解编程语言
- 递归格式化JSON数据详解编程语言
- django的中间件:process_request|process_response|process_view|process_exception详解编程语言