在我有簡單的地圖之前,例如:Map<String, Book>。我需要向這張地圖添加密鑰,所以它看起來像這樣Map<String, Map<String, Book>>在執行以下操作之前,我需要在某些條件下刪除條目:map.entrySet()
.removeIf(matches -> getMinuteDiffrenceBetweenActualAndGivenDate(ChronoUnit.MINUTES, matches.getValue()
.getDateOfCreation()) >= 20);現在我需要做同樣的事情,我不能使用,get()因為我需要迭代外部映射中的所有 value 值。我嘗試這樣做:map.entrySet().removeIf(matches -> matches.getValue().getValue()...但我不明白為什么我沒有 getValue() 方法來獲取 Book 對象。
1 回答

喵喔喔
TA貢獻1735條經驗 獲得超5個贊
matches.getValue()
是 a Map
,而不是 a Map.Entry
,所以它沒有getValue()
方法。
您可以迭代外部的值Map
并刪除內部的條目Map
:
map.values() .forEach(inner -> inner.entrySet().removeIf(matches -> getMinuteDiffrenceBetweenActualAndGivenDate(ChronoUnit.MINUTES, matches.getValue().getDateOfCreation()) >= 20));
編輯:
要刪除外部 Map 的條目:
map.entrySet() .removeIf(entry -> entry.getValue() .values() .stream() .anyMatch(book -> /*some boolean expression*/));
添加回答
舉報
0/150
提交
取消