Java 使用Comparator.reversed()反转流的排序顺序

Java 使用Comparator.reversed()反转流的排序顺序,java,java-8,java-stream,comparator,Java,Java 8,Java Stream,Comparator,我正在写一个程序,它读取一个字符串作为输入,然后输出10个重复次数最多的单词。问题是,我的顺序颠倒了。我想从最高点到最低点输出,并按相反的顺序排序。所以我一直在寻找一个解决方案,我找到的唯一一个方法是.reversed()方法,但它说“不能从静态上下文引用非静态方法”。我不理解这个错误,因为在这个例子中,它是在类似的情况下使用的 我是streams的新手,因此我想知道解决此问题的简单方法 public static void main(String[] args) { Scann

我正在写一个程序,它读取一个字符串作为输入,然后输出10个重复次数最多的单词。问题是,我的顺序颠倒了。我想从最高点到最低点输出,并按相反的顺序排序。所以我一直在寻找一个解决方案,我找到的唯一一个方法是.reversed()方法,但它说“不能从静态上下文引用非静态方法”。我不理解这个错误,因为在这个例子中,它是在类似的情况下使用的

我是streams的新手,因此我想知道解决此问题的简单方法

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        List<String> words = Arrays.asList(input.split(" "));
        words.stream()
                .map(word -> word.replaceAll("[^A-Za-z0-9]", ""))
                .map(String::toLowerCase)
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet()
                .stream()
                .sorted(Comparator.comparing(Map.Entry::getValue).reversed()) // static method can't be referenced
                .limit(10)
                .sorted(Comparator.comparing(Map.Entry::getKey))
                .forEach(e -> System.out.println(e.getKey()));
    }
publicstaticvoidmain(字符串[]args){
扫描仪=新的扫描仪(System.in);
字符串输入=scanner.nextLine();
List words=Arrays.asList(input.split(“”));
words.stream()
.map(word->word.replaceAll(“[^A-Za-z0-9]”,“”)
.map(字符串::toLowerCase)
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting())
.entrySet()
.stream()
.sorted(Comparator.comparing(Map.Entry::getValue).reversed())//无法引用静态方法
.限额(10)
.sorted(Comparator.comparing(Map.Entry::getKey))
.forEach(e->System.out.println(e.getKey());
}
编辑:我把地图的键和值混在一起,然后编辑了它。由于我的错误,Arvind的答案可能看起来有点不同,但这并不重要,因为Map.Entry同时具有.comparingByKey和.comparingByValue方法

您可以将
Comparator.reverseOrder()
传递给
Map.Entry.comparingByKey

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.function.Function;
import java.util.stream.Collectors;

class Main {
     public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in);
         String input = scanner.nextLine();
         List<String> words = Arrays.asList(input.split(" "));
         words.stream()
                 .map(word -> word.replaceAll("[^A-Za-z0-9]", ""))
                 .map(String::toLowerCase)
                 .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                 .entrySet()
                 .stream()
                 .sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())) 
                 .limit(10)
                 .sorted(Comparator.comparing(Map.Entry::getValue))
                 .forEach(e -> System.out.println(e.getKey()));
    }
}

除了下面的答案,您还可以使用
sorted((Comparator.comparing(Map.Entry::getValue)).reversed())
或者更好的方法:
sorted((Map.Entry.comparingByValue()).reversed())
值得注意的是,为什么它有效,而问题的代码无效。当涉及到链式方法调用时,类型推断具有局限性,而对于嵌套调用,它可以毫无问题地工作。
1 2 3 4 5 6 7 8 9 1 2 5 9 2 7 2 3 9 5 3 1 8 3 6 8 2 3 6 9
4
7
8
6
5
1
9
3
2