简体   繁体   English

了解 Java PriorityQueue 中的比较器

[英]Understanding Comparator in Java PriorityQueue

I have the following code:我有以下代码:

   HashMap<Character, Integer> charCount = new HashMap<Character, Integer>();
    
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        charCount.put(c, charCount.getOrDefault(c, 0) + 1);
    }
    
    PriorityQueue<Character> maxHeap = new PriorityQueue((a, b) -> charCount.get(b) - charCount.get(a));

maxHeap.addAll(charCount.keySet());

The maxHeap clearly sorts the elements to be from highest frequency to lowest but I don't understand that comparator function. maxHeap 清楚地将元素排序为从最高频率到最低频率,但我不明白比较器功能。 I find the syntax here (a, b) -> charCount.get(b) - charCount.get(a) to be quite confusing and I was hoping someone could walk through how this actually compares the elements to sort them into this maxHeap.我发现这里的语法(a, b) -> charCount.get(b) - charCount.get(a)非常令人困惑,我希望有人能够了解这实际上是如何比较元素以将它们分类到这个 maxHeap 中的。 Thanks!谢谢!

(a, b) -> charCount.get(b) - charCount.get(a)

is so-called lambda syntax.是所谓的 lambda 语法。 It's syntax sugar* for this:这是语法糖*:

new Comparator<Character>() {
    public int compare(Character a, Character b) {
        return charCount.get(b) - charCount.get(a);
    }
}

Which in turn is syntax sugar for:这反过来又是语法糖:

class MyCharacterComparator implements Comparator<Character> {
    public int compare(Character a, Character b) {
        return charCount.get(b) - charCount.get(a);
    }
}
PriorityQueue<Character> maxHeap = new PriorityQueue(new MyCharacterComparator());

But it all boils down to the same thing: It represents the concept of charCount.get(b) - charCount.get(a) .但这一切都归结为同一件事:它代表了charCount.get(b) - charCount.get(a) Not the result of that calculation, but the calculation itself .不是那个计算的结果,而是计算本身 You hand the calculation as a concept to PriorityQueue's constructor, and then PriorityQueue will invoke this code as many times as it wants, passing in whatever it needs in order to do the job.您将计算作为一个概念传递给 PriorityQueue 的构造函数,然后 PriorityQueue 将根据需要多次调用此代码,并传入完成工作所需的任何内容。

In this situation, charCount maps a given character to how often that character shows up in String s .在这种情况下, charCount将给定的字符映射到该字符在 String s频率。 The comparator will then retrieve the 'frequency' (or character count) of the second argument and the first argument, and then calculates the difference (count(b) - count(a)).然后比较器将检索第二个参数和第一个参数的“频率”(或字符数),然后计算差异(count(b) - count(a))。 The comparator method is specced to work as follows: If the result is negative, then 'a' is before 'b'.比较器方法的工作原理如下:如果结果为负,则 'a' 在 'b' 之前。 If it is positive, then 'a' is after 'b'.如果它是正数,则 'a' 在 'b' 之后。 If it is equal, than 'a' and 'b' compare on equal footing.如果相等,则 'a' 和 'b' 在同等基础上进行比较。

Thus, if the character of variable 'b' occurs, say, 10 times, and the character of variable 'a' occurs, say, 8 times, then you calculate 10 - 8 , which is a positive number, which implies that 'b' is considered to sort before a (sorts earlier; is lower).因此,如果变量 'b' 的字符出现了 10 次,而变量 'a' 的字符出现了 8 次,那么您计算10 - 8 ,这是一个正数,这意味着 'b ' 被认为在 a 之前排序(排序较早;较低)。

Thus, it prioritizes characters that appear more often.因此,它优先考虑出现频率更高的字符。

*) Not entirely; *) 不是完全; inspect the class file with eg javap and you'll find that it ends up differently, and there are also a few other differences.使用例如javap检查类文件,您会发现它的结果有所不同,并且还有一些其他差异。 But it's almost the same thing.但这几乎是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM