簡體   English   中英

從哈希映射返回與今天的日期匹配的鍵值對

[英]returning key value pair from hash map that matches todays date

我有一個包含3個參數的哈希映射aName aDate aTime

我嘗試配置一個WhatsOn方法,如果今天實際上是今天,它將僅以文本形式打印今天的鍵值對。 (並忽略所有其他鍵值對)

我想知道這是否最好通過使用date類的迭代來完成,或者是否可以不使用它來實現。 我的兩個班級如下:

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;
    }
}

您沒有利用HashMap的潛力。 如果您將日期作為關鍵,那么很容易檢索今天的所有活動。 因此,您的HashMap聲明如下:

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

為了添加活動,方法將更改為:

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
    }
}

並刪除nextId屬性。

要檢索今天的Activity列表,只需執行activities.get(aDate) 就像其他人提到的那樣,使用LocalDate代替String來作為日期。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM