简体   繁体   中英

returning key value pair from hash map that matches todays date

I have a hash map that contains 3 params aName aDate aTime

I have a WhatsOn method that I am trying to configure that will print only todays key value pair in a textual form if today is in fact today. (and disregard all other key values pairs)

I was wondering if this is best done through an iteration using the date class or can it be achieved without. My two classes are below:

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("Its saturday, just relax", "140418", "0900");

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

    //instance variables for WhatsOn class

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

    // the constructor for the WhatsOn class

    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    
    }

    public void whatsOnToday () {

     // needs configured

    }
}

Activity.java

public class Activity {

    private String name;
    private String date;
    private String time;

    //constructor

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

    //to string method
    public String toString(){
        return getName() + getDate() + getTime();
    }

    //getters and setters
    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;
    }
}

You're not using the potential of a HashMap . If you make the date the key, then it is easy to retrieve all activities of today. So your HashMap declaration would be the following:

HashMap<String, List<Activity>> activities = new HashMap<>();

In order to add an activity the method would change to:

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

    if(activities.containsKey(aDate)){
        activities.get(aDate).add(actToAdd);
    } else {
       ArrayList<Activity> activitiesList = new ArrayList<>();
       activitiesList.add(actToAdd);
       activities.put(aDate, activitiesList); //Add this instance to your Map
    }
}

And remove the nextId property.

To retrieve the list of Activity 's on today, just do activities.get(aDate) . As mentioned by others, use LocalDate instead of String s for your date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM