当前位置: 首页 > article >正文

第十三节:第四部分:集合框架: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

http://www.lryc.cn/news/2402813.html

相关文章:

  • Spring AI之RAG入门
  • 应用案例 | 设备分布广, 现场维护难? 宏集Cogent DataHub助力分布式锅炉远程运维, 让现场变“透明”
  • C#中的密封类与静态类:特性、区别与应用实例
  • LINUX 66 FTP 2 ;FTP被动模式;FTP客户服务系统
  • 网心云 OEC/OECT 笔记(2) 运行RKNN程序
  • vue-21 (使用 Vuex 模块和异步操作构建复杂应用)
  • #开发环境篇:postMan可以正常调通,但是浏览器里面一直报403
  • 将word文件转为kindle可识别的azw3文件的方法
  • 动态规划之01背包
  • Lua和JS的继承原理
  • 灵活控制,modbus tcp转ethernetip的 多功能水处理方案
  • boost::qvm 使用示例
  • go语言学习 第6章:错误处理
  • VMware 安装 CentOS8详细教程 (附步骤截图)附连接公网、虚拟机yum源等系统配置
  • Editing Language Model-based Knowledge Graph Embeddings
  • 深入了解linux系统—— 进程池
  • JavaScript 原型与原型链:深入理解 __proto__ 和 prototype 的由来与关系
  • 逻辑回归与Softmax
  • vscode .husky/pre-commit: line 4: npx: command not found
  • 光电耦合器:数字时代的隐形守护者
  • FPGA没有使用的IO悬空对漏电流有没有影响
  • 11. vue pinia 和react redux、jotai对比
  • 手机如何防止ip关联?3种低成本方案
  • Pandas和Django的示例Demo
  • 护网行动面试试题(1)
  • 【p2p、分布式,区块链笔记 MESH】Bluetooth蓝牙通信拓扑与操作 BR/EDR(经典蓝牙)和 BLE
  • 航道无人机巡检系统
  • 【JVM】Java虚拟机(一)——内存结构
  • 从微积分到集合论(1630-1910)(历史简介)——第4章——现代积分理论的起源(Thomas Hawkins)
  • 《Linux运维总结:宝德服务器RAID开启(方式一)》