s-day04Map集合存储自定义对象

This commit is contained in:
2026-08-03 10:17:30 +08:00
parent 3851e10a8f
commit 80a09fc057
2 changed files with 71 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
package com.inmind.map01;
import java.util.Objects;
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
int result = Objects.hashCode(name);
result = 31 * result + age;
return result;
}
}