如何在java8中对泛型列表进行排序

时间:2017-04-29 17:19:12

标签: java java-8 java-stream

我有一张地图Map<K, List<V>> inputMap我希望在存储到列表之前对列表进行排序

List<String> outputList = inputMap.keySet()
 .stream()
 .filter(Objects::nonNull)
 .map(key -> (key + "==" + inputMap.get(key).sort(c);))
 .sorted()
 .collect(Collectors.toList());

1 个答案:

答案 0 :(得分:3)

这个怎么样?

List<String> outputList = inputMap.keySet().stream()
            .filter(Objects::nonNull)
            .map(key -> key + "==" 
                            + inputMap.get(key).stream().sorted(c)
                                               .collect(Collectors.toList()))
            .sorted()
            .collect(Collectors.toList());

List<String> outputList = inputMap.keySet().stream()
            .filter(Objects::nonNull)
            .map(key -> key + "=="
                            + inputMap.get(key).stream().sorted(c)
                                        .map(Object::toString)
                                        .collect(Collectors.joining(", ","[","]")))
            .sorted()
            .collect(Collectors.toList());