簡體   English   中英

當使用自定義類對象作為HashMap的鍵時,從Java HashMap中隨機訪問一個值?

[英]Random access a value from Java HashMap, when using a custom class object as key for 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 Key。 我想是每一個來區分的方式course包含在對象HashMap關鍵,所以我可以得到Timings為任何隨機過程,而無需遍歷整個KeySet

提前致謝。 請詢問代碼中是否有不清楚的事情

我在這里看到的問題是

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

if (this == obj) {
            return true;
        }

因為你正在比較對象。 由於兩者都是相同的類對象,因此equals將始終返回true,而map將其作為重復鍵結束。

暫無
暫無

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

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