【从零开始学Java | 第二十七篇】HashMap、LinkedHashMap、TreeMap

张开发
2026/4/17 2:15:50 15 分钟阅读

分享文章

【从零开始学Java | 第二十七篇】HashMap、LinkedHashMap、TreeMap
前言在上一篇博客中我们学习了Map接口的特点以及常用的方法Map接口是我们处理键值对Key-Value数据的绝对主力。今天我们就来深度剖析 Java 中最常用的三大 Map 实现类HashMap、LinkedHashMap和TreeMap。一、HashMap1.HashMap的特点HashMap是Map里面的一个实现类。没有额外需要学习的特有方法直接使用Map接口里面的方法就可以了。特点都是由键决定的无序、不重复、无索引。HashMap和HashSet底层原理是一模一样的都是哈希表结构。如果键存储的是自定义对象需要重写hashCode和equals方法如果值存储的是自定义对象那么就不需要重写hashCode和equals方法。利用键计算哈希值跟值无关。2.HashMap案例创建一个HashMap集合键是学生对象Student值是籍贯String。存储几个键值对元素并遍历。要求同姓名同年龄认为是同一个学生。首先创建Student Java Bean类。package com.sprneft.day03; import java.util.Objects; public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name name; this.age age; } /** * 获取 * return name */ public String getName() { return name; } /** * 设置 * param name */ public void setName(String name) { this.name name; } /** * 获取 * return age */ public int getAge() { return age; } /** * 设置 * param age */ public void setAge(int age) { this.age age; } public String toString() { return Student{name name , age age }; } }测试类public class Test { public static void main(String[] args) { Student s1 new Student(张三, 13); Student s2 new Student(李四, 14); Student s3 new Student(王五, 15); Student s4 new Student(赵六, 16); Student s5 new Student(张三, 13); HashMapStudent, String hm new HashMap(); hm.put(s1, 广东); hm.put(s2, 上海); hm.put(s3, 湖北); hm.put(s4, 湖南); hm.put(s5, 广东); //键找值 SetStudent students hm.keySet(); for (Student s : students) { String value hm.get(s); System.out.println(s.getName() s.getAge() 岁来自 value); } } }运行结果我们看到两个姓名和年龄都相同的数据但是存储到HashMap中并没有去重这是因为我们没有在Student类中重写hashCode和equals方法。重写后结果二、LinkedHashMap1.LinkedHashMap的特点由键决定有序、不重复、无索引。这里的有序指的是保证存储和取出的元素顺序一致。原理底层数据结构依然是哈希表只是每个键值对元素又额外多了一个双链表的机制记录存储的顺序。2.LinkedHashMap案例public class Test { public static void main(String[] args) { LinkedHashMapString, Integer lhm new LinkedHashMap(); lhm.put(c, 123); lhm.put(a, 345); lhm.put(b, 153); lhm.put(a, 111); System.out.println(lhm); } }运行结果三、TreeMap1.TreeMap的特点TreeMap和TreeSet底层原理一样都是红黑树结构。由键决定特点不重复、无索引、可排序可排序指的是对键进行排序。默认按照键从小到大进行排序也可以自己规定键的顺序排序。2.TreeMap两种排序规则实现Comparable接口指定比较规则。创建集合时传递Comparator比较器对象指定比较规则。3.TreeMap案例①基本数据类型的升序、降序排列public class Test { public static void main(String[] args) { TreeMapInteger, String tm1 new TreeMap(); tm1.put(1,苹果); tm1.put(3,香蕉); tm1.put(5,菠萝); tm1.put(2,榴莲); System.out.println(tm1); TreeMapInteger, String tm2 new TreeMap(new ComparatorInteger() { Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }); tm2.put(1,苹果); tm2.put(3,香蕉); tm2.put(5,菠萝); tm2.put(2,榴莲); System.out.println(tm2); } }运行结果②自定义类型排序public class Test { public static void main(String[] args) { TreeMapStudent, String tm new TreeMap(); tm.put(new Student(张三, 13), 河北); tm.put(new Student(李四, 10), 湖南); tm.put(new Student(王五, 19), 广东); System.out.println(tm); } }运行结果总结默认首选首选HashMap它能解决你绝大部分的问题。需要记录顺序只有当你发现遍历Map时数据的顺序和你插进去的不一样且这影响了你的业务逻辑时换成LinkedHashMap。需要按大小排序当业务明确要求“给我输出排行榜”或者“按时间先后输出日志”时再考虑使用TreeMap。

更多文章