当前位置:网站首页>巧妙使用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 操作。
边栏推荐
- Using gcc to avoid stack smash attack
- How to prevent weight under Gao Bingfa?
- The relationship between cloud computing and digital transformation has finally been clarified
- [blind box app mall system] function introduction after online unpacking
- How to gracefully realize regular backup of MySQL database (glory Collection Edition)
- Is it safe to open an account on Alipay
- How to realize three schemes of finclip wechat authorized login
- what? Does the multi merchant system not adapt to app? This is coming!
- 【Pyspark基础】行转列和列转行(超多列时)
- 91. (leaflet chapter) leaflet situation plotting - offensive direction drawing
猜你喜欢

String matching (Huawei)

Conditional judgment of Shell Foundation
![[jzof] 05 replace spaces](/img/af/62432e0d6310d575a54e9409da0c32.png)
[jzof] 05 replace spaces

小程序地理位置接口申请
![[Development Tutorial 4] open source Bluetooth heart rate waterproof sports Bracelet - external flash reading and writing](/img/cf/db40dcbb141c5a2e8ccaed7700fa3e.png)
[Development Tutorial 4] open source Bluetooth heart rate waterproof sports Bracelet - external flash reading and writing

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

Gather relevant knowledge points and expand supplements
![[jzof] 06 print linked list from end to end](/img/c7/c2ac4823b5697279b81bec8f974ea9.png)
[jzof] 06 print linked list from end to end
![[image processing] pyefd.elliptic_ fourier_ How descriptors are used](/img/72/d2c825ddd95f541b37b98b2d7f6539.png)
[image processing] pyefd.elliptic_ fourier_ How descriptors are used

91. (leaflet chapter) leaflet situation plotting - offensive direction drawing
随机推荐
Volcano engine releases cloud growth solutions for six industries
What are the source database types supported by Alibaba cloud DTS?
[Matplotlib drawing]
Unfair distribution
怎么在中金证券购买新课理财产品?收益百分之6
Lenovo Filez helps Zhongshui North achieve safe and efficient file management
CAD disable a button on the toolbar (WEB version)
Is it safe for Hengtai securities to open an account?
What problems should be paid attention to when using a database without public ip: port?
支付宝上股票开户安全吗
Understand MySQL index and b+tree in an easy to understand way (supreme Collection Edition)
Practical skills!!
Huawei cloud data governance production line dataarts, let "data 'wisdom' speak"
Using gcc to avoid stack smash attack
Is it safe to open an account on Alipay
Baidu classic interview question - determine prime (how to optimize?)
Thank Huawei for sharing the developer plan
PR 2022 22.5 Chinese version
How to realize three schemes of finclip wechat authorized login
Baidu interview question - judge whether a positive integer is to the power of K of 2