简体   繁体   English

访问嵌套的 HashMap 值

[英]Access Nested HashMap Value

I have a nested hashmap that's really annoying to deal with:我有一个嵌套的 hashmap ,处理起来真的很烦人:

    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.这就是我现在正在做的,我确信这是 100% 不正确的。

        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.但我不只是想要信息,我还想找到替换它的条目,所以很难。

You can iterate by using entrySet() :您可以使用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.注意:如果您不需要过滤第一个 entrySet,您可以直接跳到第二个过滤器。 EDIT: If you need to replace the booking, you can replace the value in the final entrySet and replace the value.编辑:如果您需要替换预订,您可以替换最终 entrySet 中的值并替换该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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