I have a nested hashmap that's really annoying to deal with:
private Map<LocalDate, HashMap<Integer, HashMap<Map.Entry<Long, Long>, Booking>>> roomRecord = new HashMap<>();
I'm guessing there's a way to separate this so it's easier to work with? maybe two hashmaps instead?
Either way, what's the correct way to access the entire list of Booking? This is what I'm doing now, and I'm sure this is 100% incorrect.
List<Booking> slots = roomRecord.values().stream()
.flatMap(h -> h.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)).values().stream()
.flatMap(h -> h.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)).values().stream().collect(Collectors.toList());
but I don't just want the info, I also want to find the entry to replace it, so it's difficult.
-
Just for clarification: you need to find a specific Booking in the nested HashMap in order to replace it?vc73– vc7310/04/2021 14:14:53Commented Oct 4, 2021 at 14:14
-
@vc73 yea exactly!laila– laila10/04/2021 14:26:18Commented Oct 4, 2021 at 14:26
1 Answer 1
You can iterate by using entrySet()
:
roomRecord.entrySet()
.stream()
.filter(map -> //filter localDate and first HashMap)
.flatMap(v -> v.entrySet().stream())
.filter(map -> //filter Map.Entry and Bookings)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
.entrySet().removeIf( //remove based on your condition);
NOTE: if you don't need to filter the first entrySet, you can just skip to the second filter. EDIT: If you need to replace the booking, you can replace the value in the final entrySet and replace the value.
-
1
-
np :) Could you mark this as the answer?vc73– vc7310/05/2021 09:42:13Commented Oct 5, 2021 at 9:42