toString方法不打印哈希映射对象的文本表示

时间:2018-04-13 15:01:33

标签: java hashmap

我正在为一个小应用程序创建的哈希映射上试验toString()方法。该地图存储了三个参数aName aDate aTime

然而,当我运行for-loop时,它只打印出地图的哈希码,而不是每个键值对所需的文本表示。

for (Integer name: activities.keySet()) {
    String key =name.toString();
    String value = activities.get(name).toString();
    System.out.println(key + " " + value);
}

我想知道这个功能在哪里出错了。任何转向将不胜感激。我的2个课程在下面。

WhatsOn.java

import java.util.*;

public class WhatsOn {
    public static void main(String[] args) {
        WhatsOn alexa; // create an instance of the WhatsOn class
        alexa = new WhatsOn();
        alexa.addActivity("wash car","010117","0900");
        alexa.addActivity("go shopping","020117","1000");
        alexa.addActivity("return sale items","010117","1000");
        alexa.addActivity("back to work", "040117", "0900");

        for (Integer name: activities.keySet()) {
            String key =name.toString();
            String value = activities.get(name).toString();
            System.out.println(key + " " + value);
        }  

    }

    private String today;
    private int nextId;
    private static Map<Integer, Activity> activities;

    public WhatsOn() {
        activities = new HashMap<Integer, Activity>();
        today = "010117";
        nextId = 1;
        System.out.println("if you see this, the constructor is working");
    }

    // This method should create an instance of Activity class  and then add it to the map referenced by the current value of nextId as the key
    public void addActivity (String aName, String aDate, String aTime) {
        Activity actToAdd = new Activity(aName, aDate, aTime); //create a new instance of the activity called actToAdd, this is a local var that stores methods arguments
        activities.put(nextId, actToAdd); //Add this instance to your Map
        nextId++; //increase the nextId
    }
}

Activity.java

public class Activity {
    private String name;
    private String date;
    private String time;

    Activity(String name, String date, String time) {
        this.name = name;
        this.date = date;
        this.time = time;
    }

    public void setDate(String aDate) {
        this.date = aDate;
    }

    public void setTime(String aTime) {
        this.time = aTime;
    }

    public void setName(String aName) {
        this.name = aName;
    }

    public String getDate() {
        return this.date;
    }

    public String getTime() {
        return this.time;
    }

    public String getName() {
        return this.name;
    }
}

1 个答案:

答案 0 :(得分:1)

您需要覆盖Activity类中的toString方法。像这样:

public String toString(){
  return getName() + getDate() + getTime();
}

我建议您使用IDE的生成器功能来生成toString方法。 (另见How to override toString() properly in Java?

旁注

1)你的变量命名可能不是最好的。命名Integer&#34;名称&#34;在你的for循环中令人困惑,当一个Activity的成员变量也被称为&#34; name&#34;实际上是一个字符串。

2)在循环中,再次访问HashSet。这不是很优雅,因为你现在正在keySet上骑自行车。而是考虑在循环时获取整个EntrySet:

for (Map.Entry<Integer,Activity> entry: activities.entrySet()) {
        String key = entry.getKey().toString();
        String value = entry.getValue().toString();
        System.out.println(key + " " + value);
}