当前位置:网站首页>巧妙使用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 操作。
边栏推荐
- Unity & facegood audio2face drives face blendshape with audio
- Class notes (4) (3) -573. Lecture hall arrangement (Hall)
- How to buy Xinke financial products in CICC securities? Revenue 6%
- npm Warn config global `--global`, `--local` are deprecated. Use `--location=global` instead
- Intranet penetration learning (I) introduction to Intranet
- Selenium test page content download function
- Summary of yarn capacity scheduler
- Understand MySQL index and b+tree in an easy to understand way (supreme Collection Edition)
- 91. (leaflet chapter) leaflet situation plotting - offensive direction drawing
- Want to open an account and fry American crude oil, but always worry about insecurity?
猜你喜欢

陈春花与莫言,都有苦难言
![[image processing] pyefd.elliptic_ fourier_ How descriptors are used](/img/72/d2c825ddd95f541b37b98b2d7f6539.png)
[image processing] pyefd.elliptic_ fourier_ How descriptors are used

90% of people don't know the most underestimated function of postman!

Using skills and design scheme of redis cache (classic collection version)

运动控制如何位置同步输出
![[CCNA experiment sharing] routing between VLANs of layer 3 switches](/img/71/2f28c6b6b62f273fad1b3d71e648a1.jpg)
[CCNA experiment sharing] routing between VLANs of layer 3 switches
![[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

C # image template matching and marking

How to design the order system in e-commerce projects? (supreme Collection Edition)

Use of cache in C #
随机推荐
A simple method -- determine whether the dictionary has changed
Summary of yarn capacity scheduler
Es+redis+mysql, the high availability architecture design is awesome! (supreme Collection Edition)
Little Red Book Keyword Search commodity list API interface (commodity detail page API interface)
Sword finger offer 15. number of 1 in binary
如何修改 Kubernetes 节点 IP 地址?
Using gcc to avoid stack smash attack
2022 Tsinghua summer school notes L1_ NLP and bigmodel Foundation
Makefile基础知识--扩展
Scientific computing toolkit SciPy data interpolation
Baidu PaddlePaddle easydl helps improve the inspection efficiency of high-altitude photovoltaic power stations by 98%
Big country "grain" policy | wheat expert Liu Luxiang: China's rations are absolutely safe, and the key to increasing grain potential lies in science and technology
What is a self built database on ECs?
[jzof] 04 search in two-dimensional array
2022 Tsinghua summer school notes L2_ 2 basic introduction of CNN and RNN
【Pyspark基础】行转列和列转行(超多列时)
Experience of using dump file to reverse locate crash location
Codeforces Round #809 (Div. 2)(A~D2)
C synchronous asynchronous callback state machine async await demo
Unfair distribution