@Test
public void mich() {
Hashtable<String,List<Path>> mct = new Hashtable<String,List<Path>>();
List<Path> mm = Arrays.asList(Paths.get("File1"), Paths.get("File2"), Paths.get("File3"));
List<Path> bb = Arrays.asList(Paths.get("File4"), Paths.get("File5"), Paths.get("File6"));
List<Path> dd = Arrays.asList(Paths.get("File7"), Paths.get("File8"), Paths.get("File9"));
mct.put("A",mm);
mct.put("B",bb);
mct.put("C",dd);
List<Path> result = mct.keySet().stream().filter(s -> !s.equalIgnoreCase("C")).peek(System.out::println).map(s -> mct.get(s)).collect(Collectors.toList());
}
我希望我的结果是
File1
File2
File3
File4
File5
File6
如何映射我的路径列表?
最佳答案
你必须使用 entrySet
而不是 keySet
因为你同时使用键和值,键用于过滤,值用于获取路径,你还必须使用 flatMap像这样:
List<Path> result = mct.entrySet().stream()
.filter(e -> !"C".equalsIgnoreCase(e.getKey()))
.flatMap(e -> e.values().stream())
.collect(Collectors.toList());
result.forEach(p -> System.out.println(p.getFileName()));
输出
File1
File2
File3
File4
File5
File6
关于java - 如何通过键集从哈希表中获取所有列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51692192/