草庐IT

java - 在流上使用 Collections.toMap() 时如何保持 List 的迭代顺序?

我正在从List创建一个Map,如下所示:Liststrings=Arrays.asList("a","bb","ccc");Mapmap=strings.stream().collect(Collectors.toMap(Function.identity(),String::length));我想保持与List中相同的迭代顺序。如何使用Collectors.toMap()方法创建LinkedHashMap? 最佳答案 2-parameterversionofCollectors.toMap()使用HashMap:publicst