简体   繁体   中英

How to go through HashMap and compare the object's data and replace the value for HashMap?

I am having a hard time writing a couple of lines of code. All my current codes are in: https://github.com/anabeen/MeetingJava

The problem that I have is, finding a suitable way to go through the hashmap and get the meetings that have overlapping times and replace them.

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

Let's say we have a HashMap of [{2011-03-21=[objectMeeting1, objectMeeting2]}] and we need to add objectMeeting3 to that hashMap. How do I select the key "2011-03-21" to look at the objects in that hashmap and compare the set of objects in there with a new objectMeeting3's time (part of the data from object) and then replace that object?

In GitHub, I am trying to pass the MeetingSchedulerTest (). This is where I am stuck at:

           Meeting meeting = extractMeeting(employeeId, requestLines[i], 
                    officeStartTime, officeFinishTime, meetingSlotRequest1);
            
            

            
            if(meetings.containsKey(meetingDate)){              

                // if the order overlaps
                for (Map.Entry<LocalDate, Set<Meeting>> meetingEntry : meetings.entrySet()) {
                    if (meetingDate == meetingEntry.getKey())
                    {
                        Set<Meeting> setOfMeeting = meetingEntry.getValue();
                        for (Meeting m : setOfMeeting) {
                            
                        }
                    }
                    
                }
                
                // if the order doesn't
                
                if (meetings.get(meetingDate) != null) 
                    //shouldNotHaveOverlappingMeetings
                {                               
                    System.out.println("HERES?"); 
                    
                    meetings.remove(meetingDate);
                    Set<Meeting> meetingsForDay = new HashSet<Meeting>();
                    meetingsForDay.add(meeting);
                    meetings.put(meetingDate, meetingsForDay);
                }  else
                {
                    System.out.println("HERES2?"); 
                    meetings.get(meetingDate).add(meeting);
                }
                
                }else if (meeting != null){
                    // if meeting doens't have meetingDate then create a new HashMap with date & Meeting
                System.out.println("HERES3?");
                Set<Meeting> meetingsForDay = new HashSet<Meeting>();
                meetingsForDay.add(meeting);
                meetings.put(meetingDate, meetingsForDay);
            }
            
        }

I figured out the answer by using this:

            for (Map.Entry<LocalDate, Set<Meeting>> meetingEntry : meetings.entrySet()) {
                if (meetingDate.equals(meetingEntry.getKey()))
                {                   
                    System.out.println("HERES1? ");
                    Set<Meeting> setOfMeeting = meetingEntry.getValue();
                    for (Meeting m : setOfMeeting) {
                        System.out.println("comparing time? " + m.getStartTime().getHourOfDay() + " TO " 
                    + meeting.getStartTime().getHourOfDay());
                        if (m.compareTo(meeting) == 0) {
                            continue;
                        } else {
                            setToPut.add(m);
                        }
                    }
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM