繁体   English   中英

有人可以在这个例子中向我解释PriorityQueue吗?

[英]Can someone explain PriorityQueue in this example to me?

我正在尝试学习如何使用PriorityQueue,因为我以前从未使用过。 这是我在LeetCode上发现的一个正在使用的示例,该问题在字符串数组中找到前K个元素

public List<String> topKFrequent(String[] words, int k) {
    Map<String, Integer> count = new HashMap();
    for (String word: words) {
        count.put(word, count.getOrDefault(word, 0) + 1);
    }
    PriorityQueue<String> heap = new PriorityQueue<String>(
            (w1, w2) -> count.get(w1).equals(count.get(w2)) ?
            w2.compareTo(w1) : count.get(w1) - count.get(w2) );

    for (String word: count.keySet()) {
        heap.offer(word);
        if (heap.size() > k) heap.poll();
    }

    List<String> ans = new ArrayList();
    while (!heap.isEmpty()) ans.add(heap.poll());
    Collections.reverse(ans);
    return ans;
}

更值得注意的是,我想知道这一行在做什么:

PriorityQueue<String> heap = new PriorityQueue<String>(
            (w1, w2) -> count.get(w1).equals(count.get(w2)) ?
            w2.compareTo(w1) : count.get(w1) - count.get(w2) );

有人可以用la脚的方式解释这里发生了什么吗? 有什么方法可以将比较器重写为常规的“ if”语句?

谢谢你的帮助。

您使用的PriorityQueue构造函数声明为:

public PriorityQueue(Comparator<? super E> comparator)

它采用通用类型参数的对象的比较器。 comparator构造函数参数描述为:

用于排序此优先级队列的比较器。 如果为null,则将使用元素的自然顺序。

在您的调用中,参数是一个lambda表达式 ,提供了Comparator<String> 它大致等效于以下匿名类:

PriorityQueue<String> heap2 = new PriorityQueue<String>(new Comparator<String>() {
    @Override
    public int compare(String w1, String w2) {

        if(count.get(w1).equals(count.get(w2))) {
            return w2.compareTo(w1);
        } else {
            return count.get(w1) - count.get(w2);
        }

        // can also just be (without the if/else above):
        //return count.get(w1).equals(count.get(w2)) ? w2.compareTo(w1) : count.get(w1) - count.get(w2);
    }
});

构造函数中的表达式lambda表达式 因为Comparator是一个功能接口,也就是说,它是只有一个抽象方法的接口,所以可以使用lambda表达式作为创建匿名类的简写。

在您的示例中

new PriorityQueue<String>((w1, w2) -> count.get(w1).equals(count.get(w2)) ? w2.compareTo(w1) : count.get(w1) - count.get(w2));

在功能上等同于

new PriorityQueue<String>(new Comparator<String>() {
    public int compare(String w1, String w2) {
        return count.get(w1).equals(count.get(w2)) ? w2.compareTo(w1) : count.get(w1) - count.get(w2);
    }
});

这也与创建实现Comparator<String>的单独类,并将该类的实例作为参数传递给PriorityQueue

至于将Comparator编写为if语句,简短的答案是:不会。 比较器必须是Comparator<String>的实例。 但是,相同比较器的一个更易于理解的版本如下:

new PriorityQueue<String>((w1, w2) -> {
    if (count.get(w1).equals(count.get(w2))) { // If the counts of w1 and w2 are the same,
        return w2.compareTo(w1); // Then return the reverse lexicographical ordering of w1 and w2 (e.g. "Zebra" comes before "Apple")
    } else if (count.get(w1) < count.get(w2)) {
        return -1; // w1 comes before w2
    } else {
        return 1; // w1 comes after w2
    }
});

注意:“词法排序”本质上是字母顺序,但是基于ASCII码。 有关更多信息,请参见String#compareTo(String)

希望这可以帮助!

暂无
暂无

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

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