mara122
mara122

Reputation: 323

Removing value of nested Map in another map

Before I had simple map e.g: 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.

Upvotes: 0

Views: 728

Answers (1)

Eran
Eran

Reputation: 393811

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*/));

Upvotes: 1

Related Questions