gpt4 book ai didi

java - 从 HashMap 中返回与今天日期匹配的键值对

转载 作者:行者123 更新时间:2023-12-02 11:22:32 25 4
gpt4 key购买 nike

我有一个包含 3 个参数的 HashMap aName aDate aTime

我有一个 WhatsOn 方法,我正在尝试配置该方法,如果今天实际上是今天,则该方法将仅以文本形式打印今天的键值对。 (并忽略所有其他键值对)

我想知道这是否最好通过使用日期类的迭代来完成,或者是否可以不使用日期类来实现。我的两个类(class)如下:

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 的潜力。如果您将日期作为键,那么很容易检索今天的所有 Activity 。因此,您的 HashMap 声明如下:

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

为了添加 Activity ,该方法将更改为:

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 来表示日期。

关于java - 从 HashMap 中返回与今天日期匹配的键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49828880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com