如何使用lambda提取Map中的List值到一个新的List中?

如何使用lambda表达式提取Map<String, List<Emp>>的List<Emp>到一个新的List中?

例如:

// String是deptId

Map<String, List<Emp>> depts = new HashMap();

我想通过lambda表示将全部的Emp数据提取到一个新的List中,如何写比较优雅?

 

Map<String, List<String>> depts = new HashMap();
List<String> emps = new ArrayList<>();
depts.entrySet().stream().forEach(map -> emps.addAll(map.getValue()));

 

为什么要用Lambda呢?你知道map中list的key嘛?

Map<String, List<Emp>> depts = new HashMap();

List<Emp> list=(List<Emp>)depts.get("xxxx");

获取的时候转化下就好了.