0
static class StringMeeting {
    public String start;
    public String end;

    public StringMeeting(String start, String end) {
      this.start = start;
      this.end = end;
    }
  }

Set<StringMeeting> freeMeetingDurationSlots1 = getMeetingDurationSlots(freeSlotsCalendar1, meetingDuration);
Set<StringMeeting> freeMeetingDurationSlots2 = getMeetingDurationSlots(freeSlotsCalendar2, meetingDuration);

List<StringMeeting> commonSlots = new ArrayList<StringMeeting>();
for(StringMeeting freeMeetingDurationSlot1: freeMeetingDurationSlots1)
{
    if(freeMeetingDurationSlots2.contains(freeMeetingDurationSlot1))
                commonSlots.add(freeMeetingDurationSlot1);
}

I am trying to get the common slots and i see that freeMeetingDurationSlots2.contains(freeMeetingDurationSlot1) doesn't work. How can i say to hashset to compare based on the values of start and end time?

  • Read up on the `Set` interface, e.g. the 2nd paragraph of the JavaDocs on `Set`. This should lead you to the understanding that most sets operate using `equals()` and `hashCode()` (especially the name `HashSet` indicates the latter). Hence, implement those methods in your custom class. – Thomas Feb 15 '22 at 07:51
  • As per javadocs *Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).* - so the question is `Have you overrode equals` ? – Scary Wombat Feb 15 '22 at 07:51
  • I am not allowed to edit the custom class StringMeeting in my coding question. – Shrihari Kulkarni Feb 15 '22 at 09:57

0 Answers0