【发布时间】:2021-02-02 01:13:39
【问题描述】:
我有一个List<String> entries 列表,我想创建一个HashMap<String, Deque<Instant>> map,其中的键来自entries 列表。
我可以的
for(String s: entries){map.put(s, new Deque<>()} 但是我正在寻找更优雅的解决方案。
map = Stream.of(entries).collect(Collectors.toMap(x -> (String) x, new Deque<>()));
但是我得到了转换错误。这可以修复吗,我可以根据键列表构建地图吗?
【问题讨论】:
-
@Aman 你的解决方案将无法编译
-
entries.stream().collect(Collectors.toMap(Function.identity(), x -> new ArrayDeque()))或Collectors.toMap(Function.identity(), x -> new LinkedList())应该没问题。 @YCF_L 是的,忘了x->
标签: java dictionary collections java-8 java-stream