【问题标题】:Random access a value from Java HashMap, when using a custom class object as key for HashMap?使用自定义类对象作为 HashMap 的键时,从 Java HashMap 中随机访问一个值?
【发布时间】:2015-09-02 13:31:59
【问题描述】:

我使用自定义类对象作为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&lt;Timings&gt; 作为值。我打算在将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 密钥。我想要的是一种区分HashMap 密钥中包含的每个course 对象的方法,这样我就可以在不遍历整个KeySet 的情况下为任何随机课程获得Timings

提前致谢。请问代码中是否有不清楚的地方

【问题讨论】:

  • hm.get(course) 返回什么? null?如果是这样,您可以发布您的代码来测试这种行为吗?您的代码看起来不错,所以我怀疑是滥用地图。
  • 能否包含填充数据的代码?我们如何知道您期望看到的输出?代码看起来很好。
  • 请显示填充时间表的代码(正如 James 之前要求的那样)以及不符合预期的代码,例如一个 System.out.println(tm.get(...));以及该代码的确切输出,以及您的期望。
  • 添加了请求的代码,所以我想要的确切输出将通过提供任何课程密钥而不是遍历整个密钥集来获得时间列表。

标签: java android hashmap


【解决方案1】:

我在这里看到的问题是

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

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

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

【讨论】:

  • '==' 操作符是检查两个对象是否引用相同的内存位置。
猜你喜欢
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 2011-10-10
  • 2017-07-06
相关资源
最近更新 更多