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);
        }
    }
}
相关推荐
卷福同学3 小时前
不用服务器,不用配环境,我10分钟上线了一个AI Agent
人工智能·后端·算法
码兄科技4 小时前
Java AI智能体开发实战:从零构建智能对话系统指南
java·开发语言·人工智能
折哥的程序人生 · 物流技术专研5 小时前
第8篇:六大设计原则在工厂模式中的体现
java·设计模式·设计原则·工厂模式·编程进阶·扩充系列
至乐活着5 小时前
深入解析跳表SkipList:原理、实现与性能优化实战
数据结构·算法·跳表·skiplist·java实现
小明bishe185 小时前
计算机毕业设计之基于JAVA的植物科普网站
java·spring boot·spring·架构·课程设计
Drone_xjw5 小时前
从 GDB 到 CDB:C/C++ 程序调试的两把“手术刀”
c语言·开发语言·c++
r_oo_ki_e_5 小时前
Java Map 集合学习笔记
java·笔记·学习
SL-staff6 小时前
智慧园区2000+设备统一管理:JVS-IoT如何降低运维成本40%
java·开发语言·物联网·智慧园区·设备管理·运维优化·jvs-iot
Jerry6 小时前
LeetCode 383. 赎金信
算法
ai产品老杨6 小时前
H264 H265视频分析常见问题和排查清单
人工智能·算法·音视频