PriorityQueue使用Lambda表达式

huangapple go评论82阅读模式
英文:

PriorityQueue<Integer> with lambda expression

问题

我试图理解在Java中如何使用lambda函数与堆。下面的函数是用来创建一个最大堆。

PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);

有人能告诉我(x, y) -> y - x)是什么意思吗?
有人告诉我:“这个lambda函数将以两个整数作为输入参数,将它们相互相减,然后返回算术结果。”

所以,如果我执行以下操作:

PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);
pq.add(9);
pq.add(5);
System.out.println(pq.peek());

输出是9,因为这是一个最大堆,但是我不应该得到4作为输出吗?因为(9-5=4)?

英文:

Im trying to understand how lambda functions work with heaps in Java. the function below is to create a max heap

PriorityQueue&lt;Integer&gt; pq = new PriorityQueue&lt;&gt;((x, y) -&gt; y - x);

Can someone tell me what (x, y) -&gt; y - x) does?
Im told that "The lambda function will take two Integers as input parameters, subtract them from each other, and return the arithmetic result."

so if I do

PriorityQueue&lt;Integer&gt; pq = new PriorityQueue&lt;&gt;((x, y) -&gt; y - x);
pq.add(9);

pq.add(5);

System.out.println(pq.peek());

Output is 9 since its a max heap but should I not get 4 as an output since (9-5=4)?

答案1

得分: 4

9-5=49优先级,而 pq.peek() 返回的是而不是优先级

new PriorityQueue&lt;&gt;(comparator); 使用 (x, y) -&gt; y - x 进行比较,确定下一个应该被 peek 的值。

因此,9-5=4 大于 5-9=-4

所以值 9 将是第一个。

英文:

9-5=4 is the priority of 9, and pq.peek() returns the value, not the priority

new PriorityQueue&lt;&gt;(comparator); uses (x, y) -&gt; y - x to compare and determine which value should be next to be peeked.

So 9-5=4 is greater than 5-9=-4

So the value 9 will be the first one.

答案2

得分: 1

Sure, here's the translated content:

我正在尝试理解Java中lambda函数与堆(heaps)的工作原理。

Lambda表达式堆数据结构没有直接关系。

  • Lambda表达式 - 是一种语言特性,类似于匿名函数,可以将其用作值来使用/传递;
  • 堆 - 是一种数据结构,最初在数学中定义,在计算机科学领域广泛使用。

有人能告诉我 (x, y) -> y - x 做了什么吗?

这是你的比较器,也就是一个实现了Comparator<T>接口的lambda表达式,你将其作为参数传递给了你的优先队列(Priority Queue)构造函数。

然后,PriorityQueue<T> 使用那个比较器,并且在每次从中获取(poll/peek)一个元素时都会使用 int compare(T o1, T o2) 方法。

PriorityQueue<T> 依赖于一个契约,即

int compare(T o1, T o2) 比较它的两个参数的顺序。如果第一个参数小于、等于或大于第二个参数,则返回负整数、零或正整数。

所以,在你的代码中:

PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);
pq.add(9);
pq.add(5);
System.out.println(pq.peek());

两个数字被入队到堆/PQ中,当你 .peek() 时,95 分别传递给了 xy。由于 9-5>0,所以返回 true。


输出是9,因为这是一个最大堆,但是我不应该得到4作为输出吗,因为(9-5=4)?

9-5=4 只与比较器有关,用于决定两个元素的比较结果是否为0、正数或负数,如上所述。

英文:

>I'm trying to understand how lambda functions work with heaps in Java.

Lambda Expression has nothing to do with Heap Data Structure, per se.

  • Lambda Expression - is a language feature, acting like an anonymous function, which you can use/pass as a value;

  • Heap - is a Data Structure, originally defined in Mathematics, and nowadays heavily used in the Computer Science domains.


> Can someone tell me what (x, y) -> y - x) does?

That is your comparator, to wit, a lambda expression which implements a Comparator&lt;T&gt;, and which you pass as an argument into your priority queue constructor.

PriorityQueue&lt;T&gt; then makes use of that comparator, and utilizes int compare(T o1, T o2) method, each time you fetch (poll/peek) an element from it.

PriorityQueue&lt;T&gt; relies on a contract, that:
> int compare(T o1, T o2) Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

So, in your code:

PriorityQueue&lt;Integer&gt; pq = new PriorityQueue&lt;&gt;((x, y) -&gt; y - x);
pq.add(9);
pq.add(5);
System.out.println(pq.peek());

Two numbers are enqueued in the Heap/PQ, and when you .peek(), 9 and 5 are passed into x and y respectively. As 9-5&gt;0 true is returned.


> Output is 9 since its a max heap but should I not get 4 as an output since (9-5=4)?

9-5=4 is only relevant for the comparator, to decide whether the comparison of two elements end up in 0, positive, or negative number, as already explained above.

huangapple
  • 本文由 发表于 2020年10月9日 07:50:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64272125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定