You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java学生管理Map项目中实现自定义类的结构化打印

解决Java自定义类打印哈希地址的问题

你看到的HashmapDemo.MethodsForManagement@3d4eac69这种哈希地址,是因为Java中所有类默认继承自Object类,而Object的toString()方法默认返回的就是「类全名@对象哈希码的十六进制」。要实现结构化打印,只需要在你的StudentCinstructor类中重写toString()方法,同时还要修复现有添加方法里的潜在问题,避免数据混乱。

1. 重写StudentCinstructor的toString()方法

直接在你的学生类里添加这个方法,把对象的属性拼接成易读的字符串格式:

public class StudentCinstructor {
    // 你的类属性
    private String name;
    private double marks;
    private long mobileNo;

    // 构造方法
    public StudentCinstructor(String name, double marks, long mobileNo) {
        this.name = name;
        this.marks = marks;
        this.mobileNo = mobileNo;
    }

    // 重写toString方法,自定义输出格式
    @Override
    public String toString() {
        return "StudentCinstructor{" +
                "name='" + name + '\'' +
                ", marks=" + marks +
                ", mobileNo=" + mobileNo +
                '}';
    }

    // 可选:添加getter/setter方法,方便后续操作属性
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public double getMarks() { return marks; }
    public void setMarks(double marks) { this.marks = marks; }
    public long getMobileNo() { return mobileNo; }
    public void setMobileNo(long mobileNo) { this.mobileNo = mobileNo; }
}

这样当你打印StudentCinstructor对象时,就会输出类似StudentCinstructor{name='Alice', marks=92.5, mobileNo=1234567890}的结构化内容,而不是哈希地址了。

2. 修复添加方法的潜在问题

你当前的add()方法存在两个小问题:

  • 类级别的LinkedHashSet<StudentCinstructor> st2会被所有添加操作复用,导致所有ID对应的都是同一个集合(比如添加ID=1的学生后,再添加ID=2的学生,ID=1的集合也会包含ID=2的学生)
  • Scanner的nextDouble()/nextLong()不会读取输入后的换行符,导致下一次nextLine()会读取空字符串,引发输入异常

修改后的add()方法如下:

// 建议把HashMap定义为类成员变量
private HashMap<Integer, LinkedHashSet<StudentCinstructor>> st = new HashMap<>();

public void add() {
    Scanner sc = new Scanner(System.in);

    System.out.println("enter the name of the student");
    String name = sc.nextLine();

    System.out.println("enter the marks of the student");
    double marks = sc.nextDouble();

    System.out.println("enter the mobile number of the student");
    long mobile_no = sc.nextLong();

    // 消费nextDouble/nextLong之后的换行符,避免下次nextLine读取空内容
    sc.nextLine();

    System.out.println("enter the unique id of the student");
    int id = sc.nextInt();
    sc.nextLine(); // 同样处理换行符

    StudentCinstructor newStudent = new StudentCinstructor(name, marks, mobile_no);

    // 检查当前ID是否已存在于HashMap中
    if (st.containsKey(id)) {
        // 存在则直接将学生添加到对应的集合
        st.get(id).add(newStudent);
    } else {
        // 不存在则创建新的LinkedHashSet,添加学生后存入HashMap
        LinkedHashSet<StudentCinstructor> studentSet = new LinkedHashSet<>();
        studentSet.add(newStudent);
        st.put(id, studentSet);
    }

    System.out.println("Student added successfully!");
}

3. 测试结构化打印

在main方法中遍历HashMap打印时,就能看到清晰的结构化输出了:

public static void main(String[] args) {
    MethodsForManagement manager = new MethodsForManagement();
    manager.add();
    manager.add();

    // 遍历打印所有学生数据
    for (Map.Entry<Integer, LinkedHashSet<StudentCinstructor>> entry : manager.st.entrySet()) {
        System.out.println("\n=== Student ID: " + entry.getKey() + " ===");
        System.out.println("Student List:");
        for (StudentCinstructor student : entry.getValue()) {
            System.out.println("- " + student); // 自动调用重写的toString()
        }
    }
}

输出示例:

=== Student ID: 1 ===
Student List:
- StudentCinstructor{name='Alice', marks=95.0, mobileNo=1234567890}

=== Student ID: 2 ===
Student List:
- StudentCinstructor{name='Bob', marks=88.5, mobileNo=0987654321}

内容的提问来源于stack exchange,提问作者Ibrahim Shaikh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:46:23