简体   繁体   中英

Removing value of nested Map in another map

Before I had simple map eg: Map<String, Book> . I needed to add key to this map, so it looks like this 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.

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.

matches.getValue() is a Map , not a Map.Entry , so it has no getValue() method.

You can iterate over the values of the outer Map and remove entries of the inner 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.entrySet()
   .removeIf(entry -> entry.getValue()
                           .values()
                           .stream()
                           .anyMatch(book -> /*some boolean expression*/));

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