1

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.

asked Oct 4, 2021 at 13:54
2
  • Just for clarification: you need to find a specific Booking in the nested HashMap in order to replace it? Commented Oct 4, 2021 at 14:14
  • @vc73 yea exactly! Commented Oct 4, 2021 at 14:26

1 Answer 1

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.

answered Oct 4, 2021 at 15:08
2
  • 1
    Thank you so much!!!!! Commented Oct 4, 2021 at 18:17
  • np :) Could you mark this as the answer? Commented Oct 5, 2021 at 9:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.