当前位置:网站首页>巧妙使用sort(List<T>,Comparator<? super T>)比较器
巧妙使用sort(List<T>,Comparator<? super T>)比较器
2022-07-24 21:44:00 【华为云】
先入为主
注 sort讲解
翻翻 API 会发现, Arrays.sort 还有种重载形式:sort(T[] a, Comparator<? super T> c) ,这个方法参数的写法用到了泛型,我们还没讲到。我们可以把它理解成这样的形式: sort(Object[] a, Comparator c) ,这个方法的意思是按照比较器 c 给出的比较排序算法,对 Object 数组进行排序。Comparator 接口中定义了两个方法: compare(Object o1, Object o2) 和 equals 方法,由于 equals 方法所有对象都有的方法,因此当我们实现 Comparator 接口时,我们只需重写 compare 方法,而不需重写 equals 方法。Comparator 接口中对重写 equals 方法的描述是:“注意,不重写 Object.equals(Object) 方法总是安全的。然而,在某些情况下,重写此方法可以允许程序确定两个不同的 Comparator 是否强行实施了相同的排序,从而提高性能。”。我们只需知道第一句话就OK了,也就是说,可以不用去想应该怎么实现 equals 方法,因为即使我们不显示实现 equals 方法,而是使用Object类的 equals 方法,代码依然是安全的。
排序算法是一个经过修改的合并排序算法(其中,如果低子列表中的最高元素小于高子列表中的最低元素,则忽略合并)。此算法提供可保证的 n log(n) 性能。指定列表必须是可修改的,但不必是可大小调整的。此实现将指定列表转储到一个数组中,并对数组进行排序,在重置数组中相应位置每个元素的列表上进行迭代。这避免了由于试图原地对链接列表进行排序而产生的 n2 log(n) 性能。
参数:
list - 要排序的列表。
c - 确定列表顺序的比较器。null 值指示应该使用元素的自然顺序。
抛出:
ClassCastException - 如果列表中包含不可使用指定比较器相互比较 的元素。
UnsupportedOperationException - 如果指定列表的列表迭代器不支持 set 操作。
边栏推荐
- [Development Tutorial 4] open source Bluetooth heart rate waterproof sports Bracelet - external flash reading and writing
- [blind box app mall system] function introduction after online unpacking
- Static & dynamic & file address book
- Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]
- What should I pay attention to when selecting DTS database type?
- Overloaded & lt; for cv:: point;, But VS2010 cannot find it
- Go language pack management
- OSI的体系结构,以及各层协议
- Mathpix formula extractor
- C synchronous asynchronous callback state machine async await demo
猜你喜欢

Information System Project Manager - Chapter 10 project communication management and project stakeholder management

Penetration test - command execution injection

Selenium test page content download function

Day5: three pointers describe a tree

Intranet penetration learning (I) introduction to Intranet

Rce (no echo)

npm Warn config global `--global`, `--local` are deprecated. Use `--location=global` instead

How to gracefully realize regular backup of MySQL database (glory Collection Edition)
![[record of question brushing] 16. The sum of the nearest three numbers](/img/b9/a78b72650e94c75ccbe22af1f43857.png)
[record of question brushing] 16. The sum of the nearest three numbers
![[image processing] pyefd.elliptic_ fourier_ How descriptors are used](/img/72/d2c825ddd95f541b37b98b2d7f6539.png)
[image processing] pyefd.elliptic_ fourier_ How descriptors are used
随机推荐
Use of cache in C #
The relationship between cloud computing and digital transformation has finally been clarified
Detailed explanation of ThreadLocal
Summary of communication with customers
A simple method of converting SVG to PDF
About the acid of MySQL, there are thirty rounds of skirmishes with mvcc and interviewers
如何修改 Kubernetes 节点 IP 地址?
Understand MySQL index and b+tree in an easy to understand way (supreme Collection Edition)
CAD disable a button on the toolbar (WEB version)
【Pyspark基础】行转列和列转行(超多列时)
What should I pay attention to when choosing the self built database access method on ECs?
Is it safe to open an account on Alipay
How about opening an account for CITIC Securities? Is it safe
Selenium test page content download function
Leetcode skimming -- bit by bit record 018
Maxcompute instance related operations
Hilditch refinement (implementation I)
Multiplication and addition of univariate polynomials
How to gracefully realize regular backup of MySQL database (glory Collection Edition)
Conditional judgment of Shell Foundation