当使用自定义类对象作为HashMap的键时,从Java HashMap中随机访问一个值?

时间:2015-09-02 13:32:00

标签: java android hashmap

我使用自定义类对象作为HashMap的键。在此类定义中,我已覆盖equals()hashCode()方法。

public class TimeTableDataModel {

    Map <Course, List <Timings>> tm; 

    TimeTableDataModel() {
        tm = new HashMap<>();
    }

    void addCourseItem(Course course) {
        tm.put(course, new ArrayList<Timings>());
    }

    void addNewTimeTableItem(Course course, Timings newTiming) {
        List <Timings> t;
        if(!tm.containsKey(course)) {
            addCourseItem(course);
        }
        t = tm.get(course);
        t.add(newTiming);
        tm.put(course, t);
    }

    public static final class Course {
        private final String courseCode;
        private final String courseName;
        private final String section;
        private final String group;

        Course(String code, String courseName, String section, String group) {
            this.courseCode = code;
            this.courseName = courseName;
            this.section = section;
            this.group = group;
        }

        public String getCourseCode() { return courseCode; }
        public String getCourseName() { return courseName; }
        public String getSection() { return section; }
        public String getGroup() { return group; }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof Course)) {
                return false;
            }
            Course otherObj = (Course) obj;
            return Objects.equals(courseCode,otherObj.courseCode) 
                && Objects.equals(courseName, otherObj.courseName) 
                && Objects.equals(section, otherObj.section)
                && Objects.equals(group, otherObj.group);
        }

        @Override
        public int hashCode() {
            return Objects.hash(courseCode, courseName, section, group);
        }       
    }

    public static class Timings {
        String time;
        String day;
        String room;

        Timings(String time, String day) {
            setTime(time);
            setDay(day);
        }

        public String getTime() { return time; }
        public String getday() { return day; }

        public void setTime(String time) { this.time = time; }
        public void setDay(String day){this.day = day;}
    }
}

在上面的代码中,我创建了Course类,用作HashMap的key,并使用List<Timings>作为值。我打算在将Course传递给hm.get(course)时获取时间列表。到目前为止,我可以获得keyset,然后按顺序获取每门课程的值。

for(Course c : timetable.tm.keySet()) {
    System.out.println(c.getCourseCode() + " " + c.getCourseName());
    for(Timings t : timetable.tm.get(c)) {
        System.out.println(t.time + " " +t.room + " "+ t.day);
    }           
};

这是填充HashMap的代码

static TimeTableDataModel timetable = new TimeTableDataModel();

Course course = new Course(courseCode,null,section,group);
Timings dt = new Timings(time, getDayOfWeek(i));
dt.room  = roomNo;
timetable.addNewTimeTableItem(course, dt);

因此,为了获得特定课程的时间安排,我必须遍历整个HashMap,直到找到所需的course密钥。我想要的是一种区分course密钥中包含的每个HashMap对象的方法,因此我可以在不遍历整个Timings的情况下获得KeySet任何随机路线。

提前致谢。请询问代码中是否有不清楚的事情

1 个答案:

答案 0 :(得分:2)

我在这里看到的问题是

web.config

 if(!tm.containsKey(course)){
        addCourseItem(course);
    }

因为您正在比较对象。由于两者都是相同的类对象,因此equals将始终返回true,而map将其作为重复键结束。