简体   繁体   English

在另一个地图中移除嵌套地图的值

[英]Removing value of nested Map in another map

Before I had simple map eg: Map<String, Book> . 在我获得简单地图之前,例如: Map<String, Book> I needed to add key to this map, so it looks like this Map<String, Map<String, Book>> 我需要向此地图添加键,因此它看起来像这样的Map<String, Map<String, Book>>

I need to remove entry on some condition, before I had: 在执行以下操作之前,我需要先删除某些条目:

map.entrySet()
                .removeIf(matches -> getMinuteDiffrenceBetweenActualAndGivenDate(ChronoUnit.MINUTES, matches.getValue()
                        .getDateOfCreation()) >= 20);

Now I need to do the same, I cannot use get() as I need to iterate through all values of value in outer map. 现在,我需要做同样的事情,因为无法遍历外部映射中的所有value值,因此无法使用get()

I tried to do this way: 我试图这样做:

map.entrySet().removeIf(matches -> matches.getValue().getValue()...

but I do not understand why I do not have getValue() method to get Book object. 但是我不明白为什么我没有getValue()方法来获取Book对象。

matches.getValue() is a Map , not a Map.Entry , so it has no getValue() method. matches.getValue()是一个Map ,而不是Map.Entry ,因此它没有getValue()方法。

You can iterate over the values of the outer Map and remove entries of the inner Map : 您可以遍历外部Map的值并删除内部Map条目:

map.values()
   .forEach(inner -> inner.entrySet().removeIf(matches -> 
       getMinuteDiffrenceBetweenActualAndGivenDate(ChronoUnit.MINUTES, 
                                                   matches.getValue().getDateOfCreation()) >= 20));

EDIT: 编辑:

To remove entries of the outer Map: 要删除外部Map的条目,请执行以下操作:

map.entrySet()
   .removeIf(entry -> entry.getValue()
                           .values()
                           .stream()
                           .anyMatch(book -> /*some boolean expression*/));

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

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