【问题标题】:How to call a method for object from a set of objects?如何从一组对象中调用对象的方法?
【发布时间】:2021-12-28 01:56:24
【问题描述】:
        Map<LocalDate, Set<Meeting>> meetings = new HashMap<LocalDate, Set<Meeting>>();

我有一个哈希图,将日期存储为键,将会议类别集存储为值。

        Collection<Set<Meeting>> insideMeeting = meetings.values();
        System.out.println("meetings and their dates " + Arrays.asList(meetings));      
        System.out.println("How many meetings are there for date? " + insideMeeting);  
        
        for (Iterator<Set<Meeting>> iterator = insideMeeting.iterator(); iterator.hasNext();) {
            System.out.println("value= " + iterator.next());
            }

我正在尝试访问集合集中具有相同日期的上述会议类并进行比较。

public class Meeting implements Comparable<Meeting>{

    private String employeeId;

    private LocalTime startTime;

    private LocalTime finishTime;

    public Meeting(String employeeId, LocalTime startTime, LocalTime finishTime) {
        this.employeeId = employeeId;
        this.startTime = startTime;
        this.finishTime = finishTime;
    }


    public String getEmployeeId() {
        return employeeId;
    }

    public LocalTime getStartTime() {
        return startTime;
    }

    public LocalTime getFinishTime() {
        return finishTime;
    }

    public int compareTo(Meeting that) {
        Interval meetingInterval = new Interval(startTime.toDateTimeToday(), 
                finishTime.toDateTimeToday());
        Interval toCompareMeetingInterval = new Interval(that.getStartTime().
                toDateTimeToday(), that.getFinishTime().toDateTimeToday());

        if(meetingInterval.overlaps(toCompareMeetingInterval)){
            return 0;
        }else{
            return this.getStartTime().compareTo(that.getStartTime());
        }

    }
}

基本上,如果可以的话,我想比较会议(所以一天内没有重叠的会议。假设有人在上午 9 点到 11 点预订了会议室,而其他人从上午 10 点预订了会议- 上午 11 点。我们不想要第二个请求。)为集合中的每组会议做类似的事情,我想将它们相互比较。我在 Meeting 类中有一个 compareTo 方法。

DEBUG HERE!

meetings and their dates [{2011-03-21=[office.Meeting@4690b489, office.Meeting@363ee3a2]}]
How many meetings are there for date? [[office.Meeting@4690b489, office.Meeting@363ee3a2]]
value= [office.Meeting@4690b489, office.Meeting@363ee3a2]

【问题讨论】:

  • 听起来您知道如何获取任何一天的会议Set&lt;Meeting&gt;。所以Map 在这里实际上没有任何相关性。 (话虽如此,你的compareTo 方法违反了它的约定,并且在Set 中使用时会导致非常奇怪 的行为。)
  • @LouisWasserman 感谢您的回复。你能解释一下你所说的违反合同是什么意思吗?
  • 您不能拥有一个有效的compareTo 方法,该方法将任何重叠的时间间隔视为相等。这违反了transitivity 属性。在Set 中使用时,我只能想象会导致的错误。首先是不确定选择重叠集合中的哪一个;然后你会开始让集合中的元素实际上比较相等,然后一切都会开始崩溃。
  • @LouisWasserman 我仍然需要一种方法来比较同一日期的会议课程。您如何建议我继续访问会议课程并将第一堂课的信息与第二堂课的信息进行比较
  • 比较它们的目的是什么?

标签: java collections set


【解决方案1】:
for (var entry : meetings.values()) {
   for (Meeting meeting : entry) {
      //do whatever with meeting here
   }
}

这就是您循环遍历地图中的每个集合,然后遍历集合中的每个会议的方式。请务必记住,您的地图将包含多个集合,因此您必须使用一个键访问一个集合,或者像这里一样循环遍历所有集合。

【讨论】:

  • 是的!这就是我要找的。我只需要多一点帮助。如何将第一个会议对象的信息与第二个对象的信息进行比较?就像你如何与 array[0] 进行比较一样
  • 因为您使用集合来包含会议,所以无法直接访问集合的第 0、第 1、第 2 等元素。您将不得不创建另一个 for 循环,该循环再次循环通过同一组。老实说,解决这样的解决方案听起来相当复杂。您可以在插入会议时检查会议是否有效吗?或者您是否需要验证一个集合?如果是这样,您需要使用集合集合类型,还是可以使用数组列表?
【解决方案2】:

如果您真的想使用迭代器,我会亲自这样做。

        Iterator<Meeting> it = insideMeeting.iterator();

        while(it.hasNext()){
            Meeting current = it.next();
            it.forEachRemaining(testMeeting -> {
                if(current.getStartTime() == testMeeting.getStartTime() ||
                   current.getFinishTime() == testMeeting.getStartTime() /*Add rest of conditions*/){                               
                    //.remove whichever one you want
                }
            });
        }

虽然,正如@studmoobs 指出的那样,这有点复杂。我敢肯定,如果没有专门的一套,会有更好的方法来做到这一点。例如任何索引列表。

【讨论】:

    猜你喜欢
    • 2022-12-09
    • 2016-02-11
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    相关资源
    最近更新 更多