PriorityQueue的使用

2024-01-25 19:08
文章标签 使用 priorityqueue

本文主要是介绍PriorityQueue的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://blog.csdn.net/yangzhongblog/article/details/8607632

1 概念

  优先级队列PriorityQueue是不同于先进先出队列的另一种队列,每次从队列中取出的是具有最高优先权的元素。优先队列可用有序数组或堆实现,但是常用堆来实现,下面是2种实现效率的比较:



使用:用于获取优先权最高的元素。

例如:在1000个数中找到最大的那个数。如果用快速排序对数就行排序,时间复杂度是O(NlogN);而用优先队列(用队实现)存储数据,然后取出最大元素(此处为优先权最大的元素)这种数据结构时间复杂度为O(logN)。

2 java.util.PriorityQueue方法

  java.util.PriorityQueue是从JDK1.5开始提供的新的数据结构接口。如果不提供Comparator的话,优先队列中元素默认按自然顺序排列,也就是数字默认是小的在队列头,字符串则按字典序排列优先级队列不允许 null 元素。其提供的方法如下:


 

3 使用例子

定义存储对象

 

[java]  view plain copy
  1. <span style="font-size:18px;">package zyang.priorityQueue;  
  2.   
  3. /**  
  4.  * Fuction: 
  5.  * @version  2013-2-24 下午8:15:59  
  6.  * @since    1.0  
  7.  */  
  8.   
  9. public class Student {  
  10.       private int number;  
  11.       private String name;  
  12.         
  13.       public Student(int number,String name){  
  14.           this.number=number;  
  15.           this.name=name;  
  16.       }  
  17.   
  18.     public int getNumber() {  
  19.         return number;  
  20.     }  
  21.   
  22.     public void setNumber(int number) {  
  23.         this.number = number;  
  24.     }  
  25.   
  26.     public String getName() {  
  27.         return name;  
  28.     }  
  29.   
  30.     public void setName(String name) {  
  31.         this.name = name;  
  32.     }  
  33.   
  34.     @Override  
  35.     public String toString() {  
  36.         return "Student [number=" + number + ", name=" + name + "]";  
  37.     }   
  38. }  
  39. </span>  


定义比较器

 

 

[java]  view plain copy
  1. <span style="font-size:18px;">package zyang.priorityQueue;  
  2.   
  3. import java.util.Comparator;  
  4.   
  5. /**  
  6.  * Fuction: 
  7.  * 按学号排序 先按学号排序,学号相等时,按姓名排序 o1>o2返回-1,o1=o2返回0,o1<o2返回1  
  8.  * @author    
  9.  * @version  2013-2-24 下午8:16:26  
  10.  * @since    1.0  
  11.  */  
  12.   
  13. public class ComparetorByNumber implements Comparator {  
  14.   
  15.     public int compare(Object o1, Object o2) {  
  16.         Student s1=(Student)o1;  
  17.         Student s2=(Student)o2;  
  18.           
  19.         //compare by number  
  20.         int result=s1.getNumber() > s2.getNumber() ? 1 :(s1.getNumber() == s2.getNumber() ? 0 : -1);  
  21.         //if number is equal, then compare by name  
  22.         if (result==0){  
  23.             int compareByName=s1.getName().compareTo(s2.getName());  
  24.             if(compareByName>0)  
  25.                 result=1;  
  26.             else if(compareByName==0)  
  27.                 result=0;  
  28.             else  
  29.                 result=-1;  
  30.         } //end if  
  31.           
  32.         return (-result); //如果是result,则a>b比较结果后排序是ba,-result代表倒序排序  
  33.     }  
  34. }</span>  

 

优先队列的使用

 

[java]  view plain copy
  1. <span style="font-size:18px;">package zyang.priorityQueue;  
  2.   
  3. import java.util.PriorityQueue;  
  4.   
  5. /**  
  6.  * Fuction: 
  7.  * 
  8.  * @author   yangzhong  E-mail:yangzhonglive@gmail.com 
  9.  * @version  2013-2-24 下午8:15:39  
  10.  * @since    1.0  
  11.  */  
  12.   
  13. public class PriorityQueueApp {  
  14.   
  15.     /** 
  16.      * @param args 
  17.      */  
  18.     public static void main(String[] args) {  
  19.         PriorityQueue<Student> priorityQueue=new PriorityQueue<Student>(3,new ComparetorByNumber());  
  20.           
  21.         Student[] student={new Student(3,"wangwu"),new Student(2,"lisi"),  
  22.                 new Student(5,"xiaowang"),new Student(8,"lihua")};  
  23.           
  24.         for(Student s: student){  
  25.             priorityQueue.offer(s); //use offer() method to add elements to the PriorityQueue   
  26.         }  
  27.           
  28.         System.out.println("size: " + priorityQueue.size()); //print size  
  29.         System.out.println("peek: " + priorityQueue.peek()); //return highest priority element in the queue without removing it  
  30.         System.out.println("size: " + priorityQueue.size()); //print size  
  31.         System.out.println("poll: " + priorityQueue.poll()); //return highest priority element and removes it from the queue  
  32.         System.out.println("size: " + priorityQueue.size()); //print size  
  33.         while(!priorityQueue.isEmpty()) {  
  34.             System.out.print(priorityQueue.poll() + " ");  
  35.         }  
  36.         System.out.println("  the end!");  
  37.     }  
  38. }  
  39. </span>  

输出结果如下:

 



这篇关于PriorityQueue的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/644317

相关文章

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Java Stream 并行流简介、使用与注意事项小结

《JavaStream并行流简介、使用与注意事项小结》Java8并行流基于StreamAPI,利用多核CPU提升计算密集型任务效率,但需注意线程安全、顺序不确定及线程池管理,可通过自定义线程池与C... 目录1. 并行流简介​特点:​2. 并行流的简单使用​示例:并行流的基本使用​3. 配合自定义线程池​示

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Java 正则表达式的使用实战案例

《Java正则表达式的使用实战案例》本文详细介绍了Java正则表达式的使用方法,涵盖语法细节、核心类方法、高级特性及实战案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、正则表达式语法详解1. 基础字符匹配2. 字符类([]定义)3. 量词(控制匹配次数)4. 边

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案