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);
        }
    }
}
相关推荐
一只小青团2 小时前
Python之面向对象和类
java·开发语言
qq_529835353 小时前
ThreadLocal内存泄漏 强引用vs弱引用
java·开发语言·jvm
景彡先生3 小时前
C++并行计算:OpenMP与MPI全解析
开发语言·c++
落笔画忧愁e3 小时前
扣子Coze飞书多维表插件添加数据记录
java·服务器·飞书
量子联盟4 小时前
原创-基于 PHP 和 MySQL 的证书管理系统,免费开源
开发语言·mysql·php
秋千码途5 小时前
小架构step系列08:logback.xml的配置
xml·java·logback
飞翔的佩奇5 小时前
Java项目:基于SSM框架实现的旅游协会管理系统【ssm+B/S架构+源码+数据库+毕业论文】
java·数据库·mysql·毕业设计·ssm·旅游·jsp
时来天地皆同力.6 小时前
Java面试基础:概念
java·开发语言·jvm
hackchen6 小时前
Go与JS无缝协作:Goja引擎实战之错误处理最佳实践
开发语言·javascript·golang
找不到、了6 小时前
Spring的Bean原型模式下的使用
java·spring·原型模式