Ash*_*iya 70 java collections java-8 java-stream
我创建一个Map从List如下:
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)
hzi*_*oun 57
你可以很容易地实现通过传递你自己Supplier,Accumulator并Combiner给Supplier你的方法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)