Filter on map of map

江枫思渺然 提交于 2019-12-06 09:49:40

If you want to modify the map in place (and that it allows so), you can use forEach to iterate over the entries of the map, and then use removeIf for each values of the inner maps to remove the employees that satisfy the predicate:

employeeMap.forEach((k, v) -> v.values().removeIf(e -> e.getState().equals("MI")));

Otherwise, what you can do is to use the toMap collector, where the function to map the values takes care of removing the concerned employees by iterating over the entry set of the inner maps:

Map<String, Map<String, Employee>> employeeMap =
    employeeMap.entrySet()
               .stream()
               .collect(toMap(Map.Entry::getKey,
                              e -> e.getValue().entrySet().stream().filter(emp -> !emp.getValue().getState().equals("MI")).collect(toMap(Map.Entry::getKey, Map.Entry::getValue))));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!