當(dāng)前位置:首頁 > 公眾號(hào)精選 > 架構(gòu)師社區(qū)
[導(dǎo)讀]作者:藍(lán)筆頭鏈接:https://www.jianshu.com/p/f3e64e70eb1b1.排序1.1數(shù)組排序(`java.util.Arrays`)1.1.1基本數(shù)據(jù)類型排序?qū)φ麄€(gè)數(shù)組排序public?static?void?sort(int[]?a);對(duì)部分?jǐn)?shù)組[fro...

作者:藍(lán)筆頭
鏈接:https://www.jianshu.com/p/f3e64e70eb1b

1. 排序

1.1 數(shù)組排序(`java.util.Arrays`)

1.1.1 基本數(shù)據(jù)類型排序
  • 對(duì)整個(gè)數(shù)組排序

public?static?void?sort(int[]?a);
  • 對(duì)部分?jǐn)?shù)組 [fromIndex, toIndex) 排序

public?static?void?sort(int[]?a,?int?fromIndex,?int?toIndex);
七種基本類型 intlong、short、char、byte、float、double(除了 boolean),都支持上述格式的排序 API。

1.1.2 對(duì)象排序
  • 實(shí)現(xiàn)了 java.lang.Comparable 接口的對(duì)象。

//?對(duì)整個(gè)數(shù)組排序
public?static?void?sort(Object[]?a);
//?對(duì)部分?jǐn)?shù)組?[fromIndex,?toIndex)?排序
public?static?void?sort(Object[]?a,?int?fromIndex,?int?toIndex);
  • 通過 java.util.Comparator 排序:

void sort(T[] a, Comparator c); // 對(duì)部分?jǐn)?shù)組 [fromIndex, toIndex) 排序 public staticvoid sort(T[] a, int fromIndex, int toIndex, Comparator c); public interface Comparator{ // result < 0:o1 排在 o2 前面 // result == 0:o1 和 o2 的值一樣 // result > 0:o1 排在 o2 后面 int compare(T o1, T o2); } " data-snippet-id="ext.3b4fd31d611c03007a2fb4e82c943c31" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">//?對(duì)整個(gè)數(shù)組排序
public?static??void?sort(T[]?a,?Comparatorsuper?T>?c);
//?對(duì)部分?jǐn)?shù)組?[fromIndex,?toIndex)?排序
public?static??void?sort(T[]?a,?int?fromIndex,?int?toIndex,
????????????????????????????????Comparatorsuper
?T>?c)
;

public?interface?Comparator<T>?{
????// result
????// result ==?0:o1 和 o2 的值一樣
????// result >?0:o1 排在 o2 后面
????int?compare(T?o1,?T?o2);
}
案例:

() { @Override public int compare(Person o1, Person o2) { return o1.id - o2.id; } }); // 輸出: // Solution.Person(id=1) // Solution.Person(id=2) Arrays.stream(persons).forEach(System.out::println); } @AllArgsConstructor @ToString public static class Person { private int id; } } " data-snippet-id="ext.9f53044395f3063f1ff57b840ee3a007" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public?class?Solution?{
????public?static?void?main(String[]?args)?{
????????Person[]?persons?=?new?Person[2];
????????persons[0]?=?new?Person(2);
????????persons[1]?=?new?Person(1);

????????Arrays.sort(persons,?new?Comparator()?{
????????????@Override
????????????public?int?compare(Person?o1,?Person?o2)?{
????????????????return?o1.id?-?o2.id;
????????????}
????????});

????????//?輸出:
????????//?Solution.Person(id=1)
????????//?Solution.Person(id=2)
????????Arrays.stream(persons).forEach(System.out::println);
????}

????@AllArgsConstructor
????@ToString
????public?static?class?Person?{
????????private?int?id;
????}
}
或者使用 lambda 表達(dá)式替換 Comparator 匿名類。

{ return o1.id - o2.id; }); " data-snippet-id="ext.4973bc6f1d0213eb013aab093375a4f4" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">????????Arrays.sort(persons,?(o1,?o2)?->?{
????????????return?o1.id?-?o2.id;
????????});

1.2 列表排序(`java.util.Collections`)

  • 排序

> void sort(Listlist); public staticvoid sort(Listlist, Comparator c); public interface Comparator{ // result < 0:o1 排在 o2 前面 // result == 0:o1 和 o2 的值一樣 // result > 0:o1 排在 o2 后面 int compare(T o1, T o2); } " data-snippet-id="ext.48d424bc07d1782333e834b6ae0da4ae" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public?static?super?T>>?void?sort(List?list);

public?static??void?sort(List?list,?Comparatorsuper?T>?c);

public?interface?Comparator<T>?{
????// result
????// result ==?0:o1 和 o2 的值一樣
????// result >?0:o1 排在 o2 后面
????int?compare(T?o1,?T?o2);
}
  • 反轉(zhuǎn)列表元素

public?static?void?reverse(List?list);

1.3 二維數(shù)組排序(`java.util.Arrays`)

提示:Java 數(shù)組也是一種對(duì)象

void sort(T[] a, Comparator c); // 案例 Arrays.sort(nums, (int[]a, int[]b) -> a[0] - b[0]); " data-snippet-id="ext.d85d29980807199ad798d55caa97768a" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">//?api
public?static??void?sort(T[]?a,?Comparatorsuper?T>?c);

//?案例
Arrays.sort(nums,?(int[]a,?int[]b)?->?a[0]?-?b[0]);

2. 二分查找

  • 數(shù)組(java.util.Arrays

int binarySearch(T[] a, T key, Comparator c); public staticint binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator c); " data-snippet-id="ext.f23c7e0970895182c737a49fa0ca175a" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public?static?int?binarySearch(int[]?a,?int?key);
public?static?int?binarySearch(int[]?a,?int?fromIndex,?int?toIndex,?int?key);

public?static?int?binarySearch(Object[]?a,?Object?key);
public?static?int?binarySearch(Object[]?a,?int?fromIndex,?int?toIndex,?Object?key);

public?static??int?binarySearch(T[]?a,?T?key,?Comparatorsuper?T>?c);
public?static??int?binarySearch(T[]?a,?int?fromIndex,?int?toIndex,?T?key,?Comparatorsuper?T>?c);
  • 列表(java.util.Collections

int binarySearch(List> list, T key); public staticint binarySearch(List list, T key, Comparator c); " data-snippet-id="ext.f8d1fe37f82f9bfa0e5240865d0bfa44" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">public?static??int?binarySearch(List?extends?Comparable?super?T>>?list,?T?key);

public?static??int?binarySearch(List?extends?T>?list,?T?key,?Comparator?super?T>?c);

3. 棧(`java.util.Stack`)

  • 創(chuàng)建

Stack?stack?=?new?Stack<>();
  • 數(shù)據(jù)操作

//?往【?!坷锩嫣砑右粋€(gè)元素
public?E?push(E?item)

//?往【棧】里面彈出一個(gè)元素
public?synchronized?E?pop()
;
  • 條件判斷

public?synchronized?boolean?isEmpty();

4. 隊(duì)列(`java.util.Queue`)

  • 創(chuàng)建(java.util.LinkedList

Queue?queue?=?new?LinkedList<>();
  • 數(shù)據(jù)操作

//?往【隊(duì)列】里面添加一個(gè)元素
boolean?add(E?e);

//?往【隊(duì)列】里面彈出一個(gè)元素
E?poll();
  • 條件判斷

boolean?isEmpty();

5. 堆(`java.util.PriorityQueue`)

提示:Java 里面的優(yōu)先隊(duì)列

  • 創(chuàng)建

minHeap = new PriorityQueue<>(); // 創(chuàng)建一個(gè)最大堆 PriorityQueuemaxHeap = new PriorityQueue<>(Comparator.reverseOrder()); " data-snippet-id="ext.b81abce1e0996d64116eee23b0b4a398" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">//?創(chuàng)建一個(gè)最小堆
PriorityQueue?minHeap?=?new?PriorityQueue<>();

//?創(chuàng)建一個(gè)最大堆
PriorityQueue?maxHeap?=?new?PriorityQueue<>(Comparator.reverseOrder());
  • 數(shù)據(jù)操作

//?往【堆】里面添加一個(gè)元素
public?boolean?add(E?e);

//?從【堆】里面彈出一個(gè)元素
public?E?poll();

其他工具

  • 降序排序(java.util.Comparator

reversed(); // 反轉(zhuǎn)一個(gè) Comparable 的排序規(guī)則 // 比如從【升序】反轉(zhuǎn)為【降序】 public static> ComparatorreverseOrder(); " data-snippet-id="ext.5516f5b09794f57947cbef89383120cd" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">//?反轉(zhuǎn)一個(gè)?Comparator?的排序規(guī)則
//?比如從【升序】反轉(zhuǎn)為【降序】
default?Comparator?reversed();

//?反轉(zhuǎn)一個(gè)?Comparable?的排序規(guī)則
//?比如從【升序】反轉(zhuǎn)為【降序】
public?static?super?T>>?Comparator?reverseOrder();
  • 大數(shù)(java.math.BigInteger

//?創(chuàng)建一個(gè)大數(shù)
public?static?BigInteger?valueOf(long?val);

//?數(shù)據(jù)操作
public?BigInteger?add(BigInteger?val);
public?BigInteger?subtract(BigInteger?val);
public?BigInteger?multiply(BigInteger?val);
public?BigInteger?divide(BigInteger?val);
  • 集合(java.util.Collections

ListnCopies(int n, T o); // 反轉(zhuǎn)一個(gè) list 的順序 public static void reverse(List list); " data-snippet-id="ext.f8838765f10b2f138e21a8d0a0215ea6" data-snippet-saved="false" data-codota-status="done" style="font-size: inherit; color: inherit; line-height: inherit;">//?初始化一個(gè)具有?n?個(gè)相同元素?o?的?list
public?static??List?nCopies(int?n,?T?o);

//?反轉(zhuǎn)一個(gè)?list?的順序
public?static?void?reverse(List?list);

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡稱"軟通動(dòng)力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉