Java,hashmap,不能打印值(对象)

时间:2018-06-10 17:32:33

标签: java printing hashmap

我的代码:

HashMap<Integer,Student> Chemistry= new HashMap<Integer,Student>();
Chemistry.put(3333,"NICK");
for (int keys : Chemistry.keySet()){
             System.out.println(keys + ":"+ Chemistry.get(keys));
 }

输出是:

3333:Student@4aa298b7

1 个答案:

答案 0 :(得分:1)

这是你所看到的toString()上的默认实施。您可以覆盖它以获得更有用的东西。 E.g:

public class Student {
    private String name;

    // Constructors, methods, the whole nine yards...

    @Override
    public String toString() {
        return "A student called " + name;
    }
}