Java中的优先队列PriorityQueue如何排序

目录

一、基本数据类型的排序

(1)升序

(2)降序

二、自定义类型如何排序

(1)升序

(2)降序


既然大家想要了解优先队列的排序,那么说明已经知道什么事优先队列了,这里我就不多说了,直接说怎么排序了。

一、基本数据类型的排序

(1)升序

PriorityQueue默认的排序是从小到大。如下:

java 复制代码
import java.util.*;
import java.io.*;

public class Main {
    static PriorityQueue<Integer> queue=new PriorityQueue<>();
    public static void main(String[] args) {
        queue.add(2);
        queue.add(30);
        queue.add(17);
        queue.add(27);
        queue.add(7);

        while(!queue.isEmpty()){
            System.out.println(queue.poll());
        }
    }
}

(2)降序

java 复制代码
import java.util.*;
import java.io.*;

public class Main {
    static PriorityQueue<Integer> queue=new PriorityQueue<>(Comparator.reverseOrder());
    public static void main(String[] args) {
        queue.add(2);
        queue.add(30);
        queue.add(17);
        queue.add(27);
        queue.add(7);

        while(!queue.isEmpty()){
            System.out.println(queue.poll());
        }
    }
}

二、自定义类型如何排序

关于自定义类型,就是比如创建一个node类,其中有x和y两种属性,那么排序是按照x值还是按照y值来排序呢,这里我们就都按照x值来排序了,y值大家可以自己试试。下面我们来看看吧!

(1)升序

java 复制代码
import java.util.*;
import java.io.*;

public class Main {
    static PriorityQueue<node> q1=new PriorityQueue<node>();
    static class node implements Comparable<node>{
        int x,y;
        node(int x,int y){
            this.x=x;
            this.y=y;
        }

        @Override
        public int compareTo(node o) {
            //排序
            return this.x-o.x;
        }
    }

    public static void main(String[] args) {
        q1.add(new node(12,16));
        q1.add(new node(2,30));
        q1.add(new node(19,90));
        q1.add(new node(32,17));
        q1.add(new node(7,13));
        while(!q1.isEmpty()){
            node now=q1.poll();
            int x=now.x;
            int y=now.y;
            System.out.println("x="+x+" y="+y);
        }
    }
}

(2)降序

java 复制代码
import java.util.*;
import java.io.*;

public class Main {
    static PriorityQueue<node> q1=new PriorityQueue<node>();
    static class node implements Comparable<node>{
        int x,y;
        node(int x,int y){
            this.x=x;
            this.y=y;
        }

        @Override
        public int compareTo(node o) {
            return o.x-this.x;
        }
    }

    public static void main(String[] args) {
        q1.add(new node(12,16));
        q1.add(new node(2,30));
        q1.add(new node(19,90));
        q1.add(new node(32,17));
        q1.add(new node(7,13));
        while(!q1.isEmpty()){
            node now=q1.poll();
            int x=now.x;
            int y=now.y;
            System.out.println("x="+x+" y="+y);
        }
    }
}
相关推荐
sxlishaobin41 分钟前
Maven Setting详解
java·学习·maven
知守观1 小时前
一个方法两个触发口:反编译 XXL-Job 后,我看清了 inner/job 子包的裸奔现状
java·spring boot·微服务
用户3126874877201 小时前
CompletableFuture 链式编排:异步编程的优雅解法
java
瑞雪兆丰年兮1 小时前
JavaWeb-day14-SpringBootWeb原理篇
java·开发语言·spring boot·spring·bean·mybatis·javaweb
神仙别闹1 小时前
基于 Java GUI 贪心策略直接搜索算法和极大极小博弈树算法的智能人机博弈五子棋游戏
java·毕业设计·gui·贪心策略·极大极小博弈树
Flynn Liang2 小时前
RAG 评估体系:黄金集、检索指标与 LLM-as-Judge 落地
java·langchain·rag·评估·langchain4j·检索指标
2601_967979122 小时前
机器学习必修课:经典算法与Python实战
python·算法·机器学习·实战·课程
SomeOtherTime2 小时前
Rust生命周期介绍(AI回答)
开发语言·后端·rust
程序猿打工仔3 小时前
计算机基础(C语言)考编、专技岗
c语言·开发语言·算法
通信小呆呆3 小时前
电子战干扰技术:从“蛮力压制”到“精准欺骗”,再到“认知博弈”
算法·通信·isac