代码之家  ›  专栏  ›  技术社区  ›  weng tee

从哈希映射返回与今天日期匹配的键值对

  •  0
  • weng tee  · 技术社区  · 8 年前

    我有一个包含3个参数的哈希映射 aName aDate aTime

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

    我想知道这是通过使用date类进行迭代来实现的,还是可以不用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
    
        }
    }
    

    活动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;
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   SBylemans    7 年前

    你没有利用 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 s代表你的约会。