public class Test { public static void main(String[] args) { Map<Student,Student> map = new HashMap<>(); Student student = new Student(); map.put(student,student); map.put(student,student); student = new Student(); map.put(student,student); map.put(student,student); System.out.println(map.size()); } } class Student{} 程序的输出结果是()。
时间: 2024-04-03 17:30:55 浏览: 11
程序的输出结果是3。原因是HashMap会根据对象的hashCode值来判断是否是同一个对象,而由于第一个Student对象在put时被放进了HashMap中,后面再次put同一个对象时,由于hashCode值相同,HashMap会认为是同一个对象,所以不会将其再次放入。而第二个Student对象是一个新的对象,其hashCode值不同于第一个对象,所以HashMap会将其作为一个新的key-value对放入Map中,故Map的大小为3。
相关问题
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Test3 { public static void main(String[] args){ List<Student> list = new ArrayList<Student>(); Student s1 = new Student(001,"张三",18,"男"); Student s2 = new Student(002,"李四",19,"男"); Student s3 = new Student(003,"赵九",17,"女"); list.add(s1); list.add(s2); list.add(s3); System.out.println("请输出每个学生的信息:"); for(Student h : list){ System.out.println(h.getId() +"," + h.getName() +","+ h.getAge()+","+h.getSex()); } Map<Integer,Student> map = new HashMap<Integer,Student>(); StudentEntry se1 = new StudentEntry(s1.getId(),s1); StudentEntry se2 = new StudentEntry(s2.getId(),s2); StudentEntry se3 = new StudentEntry(s3.getId(),s3); map.put(se1.getKey(),se1.getStu()); map.put(se2.getKey(),se1.getStu()); map.put(se3.getKey(),se1.getStu()); for(Map.Entry m : map.entrySet()){ System.out.println(m.getKey() +","+ m.getValue()); } } }请帮我修改
修改后的代码如下:
```
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test3 {
public static void main(String[] args){
List<Student> list = new ArrayList<Student>();
Student s1 = new Student(001,"张三",18,"男");
Student s2 = new Student(002,"李四",19,"男");
Student s3 = new Student(003,"赵九",17,"女");
list.add(s1);
list.add(s2);
list.add(s3);
System.out.println("请输出每个学生的信息:");
for(Student h : list){
System.out.println(h.getId() +"," + h.getName() +","+ h.getAge()+","+h.getSex());
}
Map<Integer,Student> map = new HashMap<Integer,Student>();
StudentEntry se1 = new StudentEntry(s1.getId(),s1);
StudentEntry se2 = new StudentEntry(s2.getId(),s2);
StudentEntry se3 = new StudentEntry(s3.getId(),s3);
map.put(se1.getKey(),se1.getStu());
map.put(se2.getKey(),se2.getStu()); // 修改se1.getStu()为se2.getStu()
map.put(se3.getKey(),se3.getStu()); // 修改se1.getStu()为se3.getStu()
for(Map.Entry<Integer, Student> m : map.entrySet()){ // 添加泛型
System.out.println(m.getKey() +","+ m.getValue().getName()); // 输出学生姓名
}
}
}
```
修改的内容如下:
1. 第22行、23行、24行,将重复使用的`se1.getStu()`修改为`se2.getStu()`和`se3.getStu()`,修正了map中存储的学生信息不正确的问题。
2. 第26行,添加了泛型,修正了编译警告。
3. 第27行,输出学生姓名,修正了输出不完整的问题。
3.编写一个程序,把学生学号、姓名和考试分数录入到Map中,并按分数降序显示 学生的名字。要求定义Student类,封装学生学号,姓名和考试分数3个属性及方法。
下面是一个Java程序,实现了将学生学号、姓名和考试分数录入到Map中,并按分数降序显示学生的名字的功能。程序中定义了一个Student类,封装了学生学号、姓名和考试分数3个属性及方法。
```
import java.util.*;
class Student {
private int id;
private String name;
private int score;
public Student(int id, String name, int score) {
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
public class Test {
public static void main(String[] args) {
Map<Integer, Student> map = new HashMap<>();
map.put(1, new Student(1, "张三", 80));
map.put(2, new Student(2, "李四", 90));
map.put(3, new Student(3, "王五", 70));
map.put(4, new Student(4, "赵六", 85));
map.put(5, new Student(5, "小明", 95));
List<Map.Entry<Integer, Student>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Student>>() {
@Override
public int compare(Map.Entry<Integer, Student> o1, Map.Entry<Integer, Student> o2) {
return o2.getValue().getScore() - o1.getValue().getScore();
}
});
for (Map.Entry<Integer, Student> entry : list) {
System.out.println(entry.getValue().getName());
}
}
}
```
运行程序,输出结果为:
```
小明
李四
赵六
张三
王五
```