Java之PriorityQueue的使用解讀
PriorityQueue用法
一、基本概念
PriorityQueue(優(yōu)先隊(duì)列),在概念上,默認(rèn)為小頂堆,元素單調(diào)遞增排序。也可通過傳入Comparator,重寫其中的compare方法自定義排序規(guī)則;
在實(shí)現(xiàn)上,PriorityQueue實(shí)現(xiàn)了Queue接口,使用數(shù)組來存儲數(shù)據(jù),按照每層從左到右的順序存放,因此它不允許存入null值。
二、常用方法總結(jié)
| 方法 | 作用 |
|---|---|
| add(); | 隊(duì)尾插入元素,調(diào)整堆結(jié)構(gòu),失敗時拋異常 |
| offer(); | 隊(duì)尾插入元素,調(diào)整堆結(jié)構(gòu),失敗時拋false |
| remove(); | 根據(jù)value值刪除指定元素,調(diào)整堆結(jié)構(gòu),失敗時拋異常 |
| poll(); | 刪除隊(duì)頭元素,調(diào)整堆結(jié)構(gòu),失敗時拋null |
| element(); | 獲取隊(duì)列頭元素 |
| peek(); | 獲取隊(duì)列頭元素 |
| clear(); | 清空隊(duì)列 |
| size(); | 獲取隊(duì)列元素個數(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)
);
示例二、自定義一個類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問題
問題場景:從十億個數(shù)中取最大/最小的100個數(shù)
解決方案:先取100個數(shù)構(gòu)成小頂堆/大頂堆,后續(xù)每來一個數(shù)都與隊(duì)頭元素進(jìn)行判斷,比它小就直接丟棄,比它大就進(jìn)隊(duì)列中,直至訪問完畢,最后剩下的即為所求答案。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java 過濾器模式(Filter/Criteria Pattern)詳細(xì)介紹
這篇文章主要介紹了java 過濾器模式(Filter/Criteria Pattern)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-10-10
PageHelper插件實(shí)現(xiàn)一對多查詢時的分頁問題
這篇文章主要介紹了PageHelper插件實(shí)現(xiàn)一對多查詢時的分頁問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
springboot實(shí)現(xiàn)敏感字段加密存儲解密顯示功能
這篇文章主要介紹了springboot實(shí)現(xiàn)敏感字段加密存儲,解密顯示,通過mybatis,自定義注解+AOP切面,Base64加解密方式實(shí)現(xiàn)功能,本文通過代碼實(shí)現(xiàn)給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
java面試常見問題---ConcurrentHashMap
ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見問題---ConcurrentHashMap知識,一起看看吧2021-06-06

