Map接口之TreeMap
*TreeMap*
**在使用TreeSet或者TreeMap来保存自定义对象时,必须让自定义对象的类实现Comparable接口,并重写其compareTo()方法,否则会报cannot be cast to java.lang.Comparable异常。*
小案例
package com.treemappractice;
/**
* 学生实体类
*/
public class Student implements Comparable<Student>{
private String name;
private int stuNo;
public Student() {
}
public Student(String name, int stuNo) {
this.name = name;
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
//去除重复 重写hashcode方法和equals方法
//重写hashcode方法进行内存地址的判断
@Override
public int hashCode() {
int n1 = this.name.hashCode();
int n2 = this.stuNo;
return n1 + n2;
}
//必须重写equals方法才能判断添加的对象是否重复
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (obj instanceof Student) {
Student person = (Student) obj;
if (this.name.equals(person.getName()) && this.stuNo == person.getStuNo()) {
return true;
}
}
return false;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", stuNo=" + stuNo +
'}';
}
public int compareTo(Student o) {
int n2=this.stuNo-o.getStuNo();
return n2;
}
}
package com.treemappractice;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* 联系TreeMap集合的使用
* 存储结构:红黑树
*/
public class TreeMapDemo01 {
public static void main(String[] args) {
//1.添加元素 定制比较器
// TreeMap<Student, String> treeMap = new TreeMap<Student, String>(new Comparator<Student>() {
// public int compare(Student o1, Student o2) {
// return 0;
// }
// });
TreeMap<Student, String> treeMap = new TreeMap<Student, String>();
Student stu1 = new Student("孙悟空", 100);
Student stu2 = new Student("猪八戒", 101);
Student stu3 = new Student("沙和尚", 102);
treeMap.put(stu1,"金箍棒");
treeMap.put(stu2,"菜耙子");
treeMap.put(stu3,"降魔杖");
System.out.println("元素的个数:"+treeMap.size());
System.out.println("集合内容:"+treeMap);
//2.删除
// treeMap.remove(stu1);
// System.out.println("删除之后元素的个数:"+treeMap.size());
// System.out.println("删除之后集合内容:"+treeMap);
//3.遍历
System.out.println("==========keySet==========");
for (Student key : treeMap.keySet()) {
System.out.println(key+"--"+treeMap.get(key));
}
System.out.println("==========entrySet==========");
for (Map.Entry<Student, String> stringEntry : treeMap.entrySet()) {
System.out.println(stringEntry.getKey()+"--"+stringEntry.getValue());
}
}
}
运行结果:
元素的个数:3
集合内容:{Student{name='孙悟空', stuNo=100}=金箍棒, Student{name='猪八戒', stuNo=101}=菜耙子, Student{name='沙和尚', stuNo=102}=降魔杖}
==========keySet==========
Student{name='孙悟空', stuNo=100}--金箍棒
Student{name='猪八戒', stuNo=101}--菜耙子
Student{name='沙和尚', stuNo=102}--降魔杖
==========entrySet==========
Student{name='孙悟空', stuNo=100}--金箍棒
Student{name='猪八戒', stuNo=101}--菜耙子
Student{name='沙和尚', stuNo=102}--降魔杖
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
2020-01-30 Springmvc框架-json对象的处理
2020-01-30 使用Springmvc框架实现多文件上传(二)
2020-01-30 使用springmvc框架实现多文件上传