You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中Arrays.sort的@Override及PriorityQueue排序逻辑答疑

Hey there! Let's break down your questions step by step, no overly technical jargon promise 😉

一、Arrays.sort 自定义排序的实现原理

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, n1 gets placed before n2
  • If it returns a positive number, n2 gets placed before n1
  • 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:

  1. When the first element of the arrays (e.g., height) is the same: We return n1[1] - n2[1]. If n1[1] is smaller than n2[1], this returns a negative number, so n1 comes first. In short: same height? Sort by the second element in ascending order.
  2. When heights are different: We return n2[0] - n1[0]. If n2 has a taller height than n1, this returns a positive number, so n2 gets placed before n1. 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 (0 before 1)
  • Taller entries (7, then 6, then 5) come before shorter ones
  • The [5, x] entries follow the same ascending second-element rule (0 before 2)
二、PriorityQueue 降序排序的逻辑

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 a has higher priority (gets closer to the top)
  • A positive return means b has higher priority

When you use (a, b)-> b - a:

  • If b > a, this returns a positive number, so b has higher priority and moves to the top
  • If a > b, this returns a negative number, so a has lower priority and stays below

For example, if you add 3, 5, 1 to this queue:

  • The top will be 5 (highest priority), then 3, then 1
  • 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, and PriorityQueue—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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 22:53:11