App下載
首頁(yè)javastream_collectorJava Stream - 如何收集到ConcurrentMap

Java Stream - 如何收集到ConcurrentMap

我們想知道如何收集到ConcurrentMap。
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        List<String> strings = new LinkedList<>();
        strings.add("a");
        strings.add("B");
        strings.add("ab");
        strings.add("abc");
        strings.add("ABC");

        ConcurrentMap<Integer, List<String>> byLength = strings
                .parallelStream().collect(
                        Collectors.groupingByConcurrent(String::length));

        System.out.println(byLength);
    }
}