[英]returning key value pair from hash map that matches todays date
I have a hash map that contains 3 params aName
aDate
aTime
我有一个包含3个参数的哈希映射
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. 我尝试配置一个WhatsOn方法,如果今天实际上是今天,它将仅以文本形式打印今天的键值对。 (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. 我想知道这是否最好通过使用date类的迭代来完成,或者是否可以不使用它来实现。 My two classes are below:
我的两个班级如下:
WhatsOn.java 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 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
. 您没有利用
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
声明如下:
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. 并删除
nextId
属性。
To retrieve the list of Activity
's on today, just do activities.get(aDate)
. 要检索今天的
Activity
列表,只需执行activities.get(aDate)
。 As mentioned by others, use LocalDate
instead of String
s for your date. 就像其他人提到的那样,使用
LocalDate
代替String
来作为日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.