欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > 第十三节:第四部分:集合框架:HashMap、LinkedHashMap、TreeMap

第十三节:第四部分:集合框架:HashMap、LinkedHashMap、TreeMap

2025/9/23 11:31:11 来源:https://blog.csdn.net/qq_44161833/article/details/148458401  浏览:    关键词:第十三节:第四部分:集合框架:HashMap、LinkedHashMap、TreeMap

Map集合体系

Map集合体系

HashMap集合的底层原理

HashMap集合的底层原理

HashMap集合底层是基于哈希表实现的

HashMap集合底层是基于哈希表实现的

LinkedHashMap集合的底层原理

LinkedHashMap集合的底层原理

TreeMap集合的底层原理

TreeMap集合的底层原理

代码:

Student类

package com.itheima.day26_Map_impl;import java.util.Objects;public class Student implements Comparable<Student> {private String name;private int age;private double height;@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", height=" + height +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Double.compare(height, student.height) == 0 && Objects.equals(name, student.name);}@Overridepublic int hashCode() {return Objects.hash(name, age, height);}public Student() {}public Student(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}@Overridepublic int compareTo(Student o) {return this.age - o.age;//年龄升序排序}
}

代码一:掌握Map集合下的实现类:HashMap集合的底层原理

package com.itheima.day26_Map_impl;import java.util.HashMap;//目标:掌握Map集合下的实现类:HashMap集合的底层原理
public class HashMapTest1 {public static void main(String[] args) {HashMap<Student,String> map = new HashMap<>();map.put(new Student( "蜘蛛精",25,168.5),"盘丝洞");map.put(new Student( "蜘蛛精",25,168.5),"水帘洞");map.put(new Student( "至尊宝",27,178.5),"水帘洞");map.put(new Student( "牛魔王",28,188.5),"牛头山");System.out.println(map);}
}

结果1

代码二:目标:掌握LinkedHashMap的底层原理

package com.itheima.day26_Map_impl;import java.util.LinkedHashMap;
import java.util.Map;//目标:掌握LinkedHashMap的底层原理
public class LinkedHashMapTest1 {public static void main(String[] args) {Map<String,Integer> map = new LinkedHashMap<>();//按照键 有序,不重复,无索引。map.put("手机",128);map.put("手机",222);map.put("笔记本",666);map.put("手表",999);map.put(null,null);System.out.println(map);}
}

结果2

代码三:掌握TreeMap集合的使用

package com.itheima.day26_Map_impl;import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;//目标:掌握TreeMap集合的使用
public class TreeMapTest {public static void main(String[] args) {Map<Student,String> map = new TreeMap<>(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {return Double.compare(o1.getHeight(), o2.getHeight());}});map.put(new Student( "蜘蛛精",25,168.5),"盘丝洞");map.put(new Student( "蜘蛛精",25,168.5),"水帘洞");map.put(new Student( "至尊宝",27,178.5),"水帘洞");map.put(new Student( "牛魔王",28,188.5),"牛头山");System.out.println(map);}
}

结果3

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词