HashMap
案例
package com.itheima.d6;
import java.util.HashMap;
import java.util.Map;
public class Test1 {
public static void main(String[] args) {
Map<Student, String> map = new HashMap<>();
map.put(new Student("小明", 2, 1523.0), "碧蓝航线");
map.put(new Student("小明", 225, 13.0), "明日方舟");
map.put(new Student("小红", 265, 163.5), "碧蓝档案");
map.put(new Student("小王 ", 5, 123.0), "地狱公司");
map.put(new Student("小王 ", 625, 163.0), "地狱公司");
System.out.println(map);
}
}
package com.itheima.d6;
import java.util.Objects;
public class Student {
private String name;
private int age;
public double height;
@Override
public 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(student.height, height) == 0 && Objects.equals(name, student.name);
}
@Override
public 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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}
LinkedHashMap
TreeMap
package com.itheima.d6;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
public class Test33 {
public static void main(String[] args) {
Map<Student, String> map = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return Double.compare(o2.getHeight(), o1.getHeight());
}
});
map.put(new Student("小明", 2, 1523.0), "碧蓝航线");
map.put(new Student("小明", 225, 13.0), "明日方舟");
map.put(new Student("小红", 265, 163.5), "碧蓝档案");
map.put(new Student("小王 ", 5, 123.0), "地狱公司");
map.put(new Student("小王 ", 625, 163.0), "地狱公司");
System.out.println(map);
}
}
package com.itheima.d6;
import java.util.Objects;
public class Student implements Comparable<Student>{
private String name;
private int age;
public double height;
@Override
public 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(student.height, height) == 0 && Objects.equals(name, student.name);
}
@Override
public 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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
@Override
public int compareTo(Student o) {
return this.age - o.age;
}
}