Java中Arrays.sort的@Override及PriorityQueue排序逻辑答疑
Hey there! Let's break down your questions step by step, no overly technical jargon promise 😉
First off, those two Arrays.sort calls do exactly the same thing—one uses a lambda expression (Java 8+ shorthand) and the other uses an anonymous inner class. Both are just ways to define a custom sorting rule using the Comparator interface.
Let's recall how the Comparator.compare method works:
- If
compare(n1, n2)returns a negative number,n1gets placed beforen2 - If it returns a positive number,
n2gets placed beforen1 - If it returns
0, the two elements are considered equal in sorting order
Now let's unpack your specific logic:
(n2[0] == n1[0])? n1[1] - n2[1] : n2[0] - n1[0]
This is a ternary operator that translates to:
- When the first element of the arrays (e.g., height) is the same: We return
n1[1] - n2[1]. Ifn1[1]is smaller thann2[1], this returns a negative number, son1comes first. In short: same height? Sort by the second element in ascending order. - When heights are different: We return
n2[0] - n1[0]. Ifn2has a taller height thann1, this returns a positive number, son2gets placed beforen1. In short: different heights? Sort in descending order of height.
This perfectly matches your input-output example:
- The two
[7, x]entries are sorted by the second element (0before1) - Taller entries (
7, then6, then5) come before shorter ones - The
[5, x]entries follow the same ascending second-element rule (0before2)
By default, PriorityQueue in Java is a min-heap—meaning the smallest element sits at the top, and poll() always returns the smallest value. But your code changes this to a max-heap with the lambda (a, b)-> b - a.
Let's break down how this comparator works for the queue:
The comparator defines the "priority" of elements. For compare(a, b):
- A negative return means
ahas higher priority (gets closer to the top) - A positive return means
bhas higher priority
When you use (a, b)-> b - a:
- If
b > a, this returns a positive number, sobhas higher priority and moves to the top - If
a > b, this returns a negative number, soahas lower priority and stays below
For example, if you add 3, 5, 1 to this queue:
- The top will be
5(highest priority), then3, then1 - Every time you call
poll(), you'll get the largest remaining element—this is how you achieve descending-order behavior.
Compare this to the default comparator (a, b)-> a - b, which makes the queue a min-heap (smallest element at the top).
Here are some great resources to deepen your understanding:
- Java Official Documentation: Check out the sections for
Comparator,Arrays.sort, andPriorityQueue—it's the most authoritative source, with clear method explanations and examples. - Core Java Volume I – Fundamentals: This book has in-depth, easy-to-follow chapters on collections, sorting, lambda expressions, and object-oriented principles. It's a must-have for any Java developer.
- Oracle Java Tutorials (Collections Section): Free, structured tutorials that cover sorting, queues, and other collection basics with hands-on examples.
- Effective Java: This book shares best practices for writing clean, efficient Java code—including tips on using lambdas and comparators effectively.
内容的提问来源于stack exchange,提问作者SUVAIN G

