【发布时间】:2019-06-13 06:05:07
【问题描述】:
我正在使用 Maps 集合创建学生管理简单的 java 项目,其中 id 是我的密钥,名称、标记和手机号码。是地图的值。那么如何以结构化的方式打印它。
HashMap<Integer, LinkedHashSet<StudentCinstructor>> st = new HashMap<>();
LinkedHashSet<StudentCinstructor> st2 = new LinkedHashSet<>();
Scanner sc = new Scanner(System.in);
public void add() {
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();
st2.add(new StudentCinstructor(name, marks, mobile_no));
System.out.println("enter the unique id of the student");
int id = sc.nextInt();
st.put(id, st2);
当我尝试在主方法中打印自定义类时,它给了我一个带有哈希码的地址。 "HashmapDemo.MethodsForManagement@3d4eac69"
【问题讨论】:
-
请edit您的问题包括minimal reproducible example。另请查看
Object.toString()和System.out.println(Object)的文档以了解您获得的输出。 -
单个id有多个值吗
-
不清楚为什么您使用
StudentCinstructor的sets 作为地图的值,而不是单独的StudentCinstructor对象。由于地图是通过所提供的add()方法填充的,因此在地图中用作值的所有集合都不会只有一个元素。
标签: java hashmap linkedhashset