PriorityQueue优先级队列
2017-04-11 13:22
363 查看
优先级队列是不同于先进先出队列的另一种队列。每次从队列中取出的是具有最高优先权的元素。
PriorityQueue是从JDK1.5开始提供的新的数据结构接口。
如果不提供Comparator的话,优先队列中元素默认按自然顺序排列,也就是数字默认是小的在队列头,字符串则按字典序排列。
由于网上的资料大多将优先级队列各个方法属性,很少有实例讲解的,为方便大家以后使用,我就写了个demo~
如果想实现按照自己的意愿进行优先级排列的队列的话,需要实现Comparator接口。下面的方法,实现了根据某个变量,来进行优先级队列的建立。
[java]
view plain
copy
print?
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
public class test {
private String name;
private int population;
public test(String name, int population)
{
this.name = name;
this.population = population;
}
public String getName()
{
return this.name;
}
public int getPopulation()
{
return this.population;
}
public String toString()
{
return getName() + " - " + getPopulation();
}
public static void main(String args[])
{
Comparator<test> OrderIsdn = new Comparator<test>(){
public int compare(test o1, test o2) {
// TODO Auto-generated method stub
int numbera = o1.getPopulation();
int numberb = o2.getPopulation();
if(numberb > numbera)
{
return 1;
}
else if(numberb<numbera)
{
return -1;
}
else
{
return 0;
}
}
};
Queue<test> priorityQueue = new PriorityQueue<test>(11,OrderIsdn);
test t1 = new test("t1",1);
test t3 = new test("t3",3);
test t2 = new test("t2",2);
test t4 = new test("t4",0);
priorityQueue.add(t1);
priorityQueue.add(t3);
priorityQueue.add(t2);
priorityQueue.add(t4);
System.out.println(priorityQueue.poll().toString());
}
}
PriorityQueue是从JDK1.5开始提供的新的数据结构接口。
如果不提供Comparator的话,优先队列中元素默认按自然顺序排列,也就是数字默认是小的在队列头,字符串则按字典序排列。
由于网上的资料大多将优先级队列各个方法属性,很少有实例讲解的,为方便大家以后使用,我就写了个demo~
如果想实现按照自己的意愿进行优先级排列的队列的话,需要实现Comparator接口。下面的方法,实现了根据某个变量,来进行优先级队列的建立。
[java]
view plain
copy
print?
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
public class test {
private String name;
private int population;
public test(String name, int population)
{
this.name = name;
this.population = population;
}
public String getName()
{
return this.name;
}
public int getPopulation()
{
return this.population;
}
public String toString()
{
return getName() + " - " + getPopulation();
}
public static void main(String args[])
{
Comparator<test> OrderIsdn = new Comparator<test>(){
public int compare(test o1, test o2) {
// TODO Auto-generated method stub
int numbera = o1.getPopulation();
int numberb = o2.getPopulation();
if(numberb > numbera)
{
return 1;
}
else if(numberb<numbera)
{
return -1;
}
else
{
return 0;
}
}
};
Queue<test> priorityQueue = new PriorityQueue<test>(11,OrderIsdn);
test t1 = new test("t1",1);
test t3 = new test("t3",3);
test t2 = new test("t2",2);
test t4 = new test("t4",0);
priorityQueue.add(t1);
priorityQueue.add(t3);
priorityQueue.add(t2);
priorityQueue.add(t4);
System.out.println(priorityQueue.poll().toString());
}
}
import java.util.Comparator; import java.util.PriorityQueue; import java.util.Queue; public class test { private String name; private int population; public test(String name, int population) { this.name = name; this.population = population; } public String getName() { return this.name; } public int getPopulation() { return this.population; } public String toString() { return getName() + " - " + getPopulation(); } public static void main(String args[]) { Comparator<test> OrderIsdn = new Comparator<test>(){ public int compare(test o1, test o2) { // TODO Auto-generated method stub int numbera = o1.getPopulation(); int numberb = o2.getPopulation(); if(numberb > numbera) { return 1; } else if(numberb<numbera) { return -1; } else { return 0; } } }; Queue<test> priorityQueue = new PriorityQueue<test>(11,OrderIsdn); test t1 = new test("t1",1); test t3 = new test("t3",3); test t2 = new test("t2",2); test t4 = new test("t4",0); priorityQueue.add(t1); priorityQueue.add(t3); priorityQueue.add(t2); priorityQueue.add(t4); System.out.println(priorityQueue.poll().toString()); } }
相关文章推荐
- 基于堆实现的优先级队列:PriorityQueue 解决 Top K 问题
- PriorityQueue优先级队列及优先级阻塞队列
- java之_队列(PriorityQueue 优先级队列)
- jiava中的PriorityQueue(优先级队列)
- Java优先级队列PriorityQueue
- PriorityQueue--优先级队列
- Android PriorityQueue优先级队列 学习!
- JavaScript 数据结构(4):优先级队列 PriorityQueue
- java之中PriorityQueue实现原理(具有优先级的队列)
- PriorityQueue 优先级队列【java】
- PriorityQueue 优先级队列
- PriorityQueue是个基于优先级堆的极大优先级队列
- PriorityQueue优先级队列 学习
- 优先级队列——PriorityQueue
- 优先级队列PriorityQueue
- 优先级队列:PriorityQueue
- 算法:优先级队列(PriorityQueue)
- java中自己用过的数据结构(队列Queue、优先级队列PriorityQueue和栈Stack),及其分别的应用场景
- JDK中优先级队列PriorityQueue实现分析
- 优先级队列:PriorityQueue