我有一个列表,需要将其转换为映射(Map),但需要保持键的顺序不变,因此需要转换成LinkedHashMap。我需要像下面这样的东西:
可以将以上两种变体结合起来吗?
list.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
但是有具体类型的地图,例如:
list.stream().collect(Collectors.toCollection(LinkedHashMap::new))
可以将以上两种变体结合起来吗?
(v1,v2)->v1
,而是使用(v1,v2) -> { throw new AssertionError("keys should already be unique"); }
,这样更好,可以确保键已经唯一。 - Holger