我有一个日期列表作为请求:
requestDateRange = ["2020-09-10","2020-05-06","2020-04-11"]
我想将此列表转换为映射,键为映射,值为空列表,稍后将填充值。
2020-09-10 -> []
2020-05-06 -> []
2020-04-11 -> []
我所做的如下:
Map<LocalDate, HashSet<String>> myMap = new HashMap<>();
for (LocalDate date : requestedDateRange){
myMap.put(date, new HashSet<>());
}
使用 hashSet 只有唯一值
我怎样才能以更好的方式或使用 Java8 特性做到这一点?
这应该可以解决问题:
Map<LocalDate, List<String>> myMap =
requestedDateRange.stream()
.collect(toMap(identity(), d -> new ArrayList<>()));
或者这个:
Map<LocalDate, Set<String>> myMap =
requestedDateRange.stream()
.collect(toMap(identity(), d -> new HashSet<>()));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句