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

Ash*_*iya 70 java collections java-8 java-stream

我创建一个MapList如下:

List<String> strings = Arrays.asList("a", "bb", "ccc");

Map<String, Integer> map = strings.stream()
    .collect(Collectors.toMap(Function.identity(), String::length));
Run Code Online (Sandbox Code Playgroud)

我希望保持与之相同的迭代顺序List.如何创建LinkedHashMap使用Collectors.toMap()方法?

pru*_*nge 95

2个参数的版本Collectors.toMap()采用的是HashMap:

public static <T, K, U> Collector<T, ?, Map<K,U>> toMap(
    Function<? super T, ? extends K> keyMapper, 
    Function<? super T, ? extends U> valueMapper) 
{
    return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new);
}
Run Code Online (Sandbox Code Playgroud)

要使用4参数版本,您可以替换:

Collectors.toMap(Function.identity(), String::length)
Run Code Online (Sandbox Code Playgroud)

有:

Collectors.toMap(
    Function.identity(), 
    String::length, 
    (u, v) -> {
        throw new IllegalStateException(String.format("Duplicate key %s", u));
    }, 
    LinkedHashMap::new
)
Run Code Online (Sandbox Code Playgroud)

或者为了使它更清洁,编写一个新toLinkedMap()方法并使用它:

public class MoreCollectors
{
    public static <T, K, U> Collector<T, ?, Map<K,U>> toLinkedMap(
        Function<? super T, ? extends K> keyMapper,
        Function<? super T, ? extends U> valueMapper)
    {
        return Collectors.toMap(
            keyMapper,
            valueMapper, 
            (u, v) -> {
                throw new IllegalStateException(String.format("Duplicate key %s", u));
            },
            LinkedHashMap::new
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

  • `mergeFunction` 在 4 参数版本 `Collectors.toMap()` 中不知道要合并哪个键,`u` 和 `v` 都是值。所以关于 IllegalStateException 的消息并不正确。 (3认同)
  • 为什么这么复杂?您可以轻松做到,请在下面查看我的答案 (2认同)

hzi*_*oun 57

你可以很容易地实现通过传递你自己Supplier,AccumulatorCombinerSupplier你的方法Accumulator象下面这样:

List<String> myList = Arrays.asList("a", "bb", "ccc"); 
// or since java 9 List.of("a", "bb", "ccc");

LinkedHashMap<String, Integer> mapInOrder = myList
    .stream()
    .collect(
        LinkedHashMap::new,                                   // Supplier
        (map, item) -> map.put(item, item.length()),          // Accumulator
        Map::putAll);                                         // Combiner

System.out.println(mapInOrder);  // {a=1, bb=2, ccc=3}
Run Code Online (Sandbox Code Playgroud)

  • 该答案应标记为已接受答案。 (6认同)