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

25 浏览
0 Comments

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

我正在从一个List中创建一个Map,代码如下:

List strings = Arrays.asList("a", "bb", "ccc");
Map map = strings.stream()
    .collect(Collectors.toMap(Function.identity(), String::length));

我想保持与List中相同的迭代顺序。我如何使用Collectors.toMap()方法创建一个LinkedHashMap

0