Home > Software design >  How to call a method for object from a set of objects?
How to call a method for object from a set of objects?

Time:11-17

        Map<LocalDate, Set<Meeting>> meetings = new HashMap<LocalDate, Set<Meeting>>();

I have a hashmap that stores the date as key and Set of Meeting Class as value.

        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());
            }

And I am trying to access the said Meeting classes with the same date inside the collection set and compare them.

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());
        }

    }
}

Basically, if I could I want to compare the meetings (so there are no overlapping meetings in a single day. Let's say someone booked a room for a meeting at 9 am - 11 am and someone else booked the meeting from 10 am - 11 am. We wouldn't want the second request.) Do something like for each set of meetings in the collection I want to compare them with each other. I do have a compareTo method in the Meeting class.

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]

CodePudding user response:

for (var entry : meetings.values()) {
   for (Meeting meeting : entry) {
      //do whatever with meeting here
   }
}

This is how you would loop through each set in your map, and then through each Meeting in your set. It's important to remember that your map will contain several collections, and so you must access one using a key or loop through all of them like here.

CodePudding user response:

This is how I'd personally do it if you really wanted to use an iterator.

        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
                }
            });
        }

  • Related