Java之PriorityQueue的使用解讀
PriorityQueue用法
一、基本概念
PriorityQueue(優(yōu)先隊(duì)列),在概念上,默認(rèn)為小頂堆,元素單調(diào)遞增排序。也可通過(guò)傳入Comparator,重寫其中的compare方法自定義排序規(guī)則;
在實(shí)現(xiàn)上,PriorityQueue實(shí)現(xiàn)了Queue接口,使用數(shù)組來(lái)存儲(chǔ)數(shù)據(jù),按照每層從左到右的順序存放,因此它不允許存入null值。
二、常用方法總結(jié)
| 方法 | 作用 |
|---|---|
| add(); | 隊(duì)尾插入元素,調(diào)整堆結(jié)構(gòu),失敗時(shí)拋異常 |
| offer(); | 隊(duì)尾插入元素,調(diào)整堆結(jié)構(gòu),失敗時(shí)拋false |
| remove(); | 根據(jù)value值刪除指定元素,調(diào)整堆結(jié)構(gòu),失敗時(shí)拋異常 |
| poll(); | 刪除隊(duì)頭元素,調(diào)整堆結(jié)構(gòu),失敗時(shí)拋null |
| element(); | 獲取隊(duì)列頭元素 |
| peek(); | 獲取隊(duì)列頭元素 |
| clear(); | 清空隊(duì)列 |
| size(); | 獲取隊(duì)列元素個(gè)數(shù) |
| contains(); | 判斷隊(duì)列中是否包含指定元素 |
| isEmpty(); | 判斷隊(duì)列是否為空 |
三、具體使用
1、實(shí)現(xiàn)降序排列(大頂堆)
方式一、lambda表達(dá)式
PriorityQueue<Integer> queue = new PriorityQueue<>(
(o1, o2) -> o2 - o1
);
方式二、重寫compare方法
PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
2、實(shí)現(xiàn)自定義排序
示例一、按字符串的第三位進(jìn)行降序排列
PriorityQueue<String> queue = new PriorityQueue<>(
(o1, o2) -> o2.charAt(2) - o1.charAt(2)
);
示例二、自定義一個(gè)類People,先按名字排序,再按年齡排序,再按身高排序
public class Solution {
public static void main(String[] args) {
PriorityQueue<People> queue = new PriorityQueue<>(
(o1, o2) -> {
if (o1.getName().compareTo(o2.getName()) > 0) {
return 1;
} else if (o1.getName().compareTo(o2.getName()) < 0) {
return -1;
} else {
if (o1.getAge() > o2.getAge()) {
return 1;
} else if (o1.getAge() < o2.getAge()) {
return -1;
} else {
if (o1.getHeight() - o2.getHeight() > 0) {
return 1;
} else if (o1.getHeight() - o2.getHeight() < 0) {
return -1;
} else {
return 0;
}
}
}
}
);
People one = new People("one", 12, 45.6f);
People two = new People("one", 12, 12.3f);
queue.add(one);
queue.add(two);
for (People tmp : queue) {
System.out.println(tmp);
}
}
}
class People {
private String name;
private int age;
private float height;
public People(String name, int age, float height) {
this.name = name;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
@Override
public String toString() {
return "people{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}
3、解決TOP K問(wèn)題
問(wèn)題場(chǎng)景:從十億個(gè)數(shù)中取最大/最小的100個(gè)數(shù)
解決方案:先取100個(gè)數(shù)構(gòu)成小頂堆/大頂堆,后續(xù)每來(lái)一個(gè)數(shù)都與隊(duì)頭元素進(jìn)行判斷,比它小就直接丟棄,比它大就進(jìn)隊(duì)列中,直至訪問(wèn)完畢,最后剩下的即為所求答案。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA maven項(xiàng)目中刷新依賴的兩種方法小結(jié)
這篇文章主要介紹了IDEA maven項(xiàng)目中刷新依賴的兩種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring中的ApplicationRunner接口的使用詳解
這篇文章主要介紹了Spring中的ApplicationRunner接口的使用詳解,ApplicationRunner使用起來(lái)很簡(jiǎn)單,只需要實(shí)現(xiàn)CommandLineRunner或者ApplicationRunner接口,重寫run方法就行,需要的朋友可以參考下2023-11-11
spring-boot-starter-web更換默認(rèn)Tomcat容器的方法
Spring Boot支持容器的自動(dòng)配置,默認(rèn)是Tomcat,當(dāng)然我們也是可以進(jìn)行修改的。下面小編給大家?guī)?lái)了spring-boot-starter-web更換默認(rèn)Tomcat容器的方法,感興趣的朋友跟隨小編一起看看吧2019-04-04
Java中StringRedisTemplate和RedisTemplate的區(qū)別及使用方法
本文主要介紹了Java中StringRedisTemplate和RedisTemplate的區(qū)別及使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
java volatile關(guān)鍵字作用及使用場(chǎng)景詳解
在本文里我們給大家分享的是關(guān)于java volatile關(guān)鍵字作用及使用場(chǎng)景的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-08-08
分布式組件Gateway技術(shù)棧系統(tǒng)性理解和操作
這篇文章主要介紹了分布式組件Gateway技術(shù)棧系統(tǒng)性理解和操作,要真正理解和使用Gateway技術(shù)棧,需要從架構(gòu)認(rèn)知、核心機(jī)制、實(shí)踐應(yīng)用三個(gè)維度建立系統(tǒng)性理解,需要的朋友可以參考下2026-02-02
Maven setting配置鏡像倉(cāng)庫(kù)的方法步驟
這篇文章主要介紹了Maven setting配置鏡像倉(cāng)庫(kù)的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12

