# Java PriorityQueue的使用方法

290人阅读 评论(0)

package stl;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;

public class Test10 {

public static void main(String[] args)
{
test();
test2();
}

public static void test()
{
List intList = Arrays.asList(1,2,3,9,4,3,6,0,0,1);

PriorityQueue<Integer> intQueue = new PriorityQueue<Integer>(intList);
System.out.println(intQueue); //存储顺序 [0, 0, 3, 1, 1, 3, 6, 9, 2, 4]
Iterator<Integer> intIteator = intQueue.iterator();
while (intIteator.hasNext()) {
System.out.print(intIteator.next() + ",");  //存储顺序 0,0,3,1,1,3,6,9,2,4,
}
System.out.println();
while (!intQueue.isEmpty()) {
System.err.print(intQueue.poll() + ",");   //排队顺序 0,0,1,1,2,3,3,4,6,9,
}
System.out.println();System.out.println();

intQueue = new PriorityQueue<>(intList.size(), Collections.reverseOrder());
System.out.println(intQueue); //存储顺序 [9, 4, 6, 1, 3, 2, 3, 0, 0, 1]
intIteator = intQueue.iterator();
while (intIteator.hasNext()) {
System.out.print(intIteator.next() + ","); //存储顺序 9,4,6,1,3,2,3,0,0,1,
}
System.out.println();
while (!intQueue.isEmpty()) {
System.err.print(intQueue.poll() + ","); //排队顺序 9,6,4,3,3,2,1,1,0,0,
}
System.out.println();System.out.println();

String s = "ABCD EFGH 0023 982";

PriorityQueue<String> strQueue = new PriorityQueue<String>(Arrays.asList(s.split("")));
System.out.println(strQueue);  // [,  ,  , 2,  , 0, 2, 9, 8, H, D, B, 0, E, 3, F, C, A, G]
Iterator<String> strIteator = strQueue.iterator();
while (strIteator.hasNext()) {
System.out.print(strIteator.next() + ","); // , , ,2, ,0,2,9,8,H,D,B,0,E,3,F,C,A,G,
}
System.out.println();System.out.println();
while (!strQueue.isEmpty()) {
System.err.print(strQueue.poll() + ","); // , , , ,0,0,2,2,3,8,9,A,B,C,D,E,F,G,H,
}
System.out.println();

strQueue = new PriorityQueue<>(s.split("").length, Collections.reverseOrder());
System.out.println(strQueue); // [H, G, D, E, F, 0, A, 9, C, B,  ,  , 0, 2, 3, ,  , 8, 2]
strIteator = strQueue.iterator();
while (strIteator.hasNext()) {
System.out.print(strIteator.next() + ",");		 // H,G,D,E,F,0,A,9,C,B, , ,0,2,3,, ,8,2,
}
System.out.println();
while (!strQueue.isEmpty()) {
System.err.print(strQueue.poll() + ","); //H,G,F,E,D,C,B,A,9,8,3,2,2,0,0, , , ,,
}
System.out.println();System.out.println();
}

public static void test2()
{
PriorityQueue<Student> queue = new PriorityQueue<Student>(11,
new Comparator<Student>()
{
public int compare(Student s1, Student s2) {
}
}
);

for (int i = 1; i <= 100; i++)
{
queue.add(new Student("s" + i, (new Random().nextInt(1000))));
}
System.out.println(queue);
while (!queue.isEmpty())
{
System.err.print(queue.poll());
}
System.out.println();System.out.println();
}

}

class Student {
String name;
{
this.name = name;
}
public String toString()
{
return "[" + name + " " + grade + "], ";
}
}


[0, 0, 3, 1, 1, 3, 6, 9, 2, 4]
0,0,3,1,1,3,6,9,2,4,
0,0,1,1,2,3,3,4,6,9,

[9, 4, 6, 1, 3, 2, 3, 0, 0, 1]
9,4,6,1,3,2,3,0,0,1,
9,6,4,3,3,2,1,1,0,0,

[,  ,  , 2,  , 0, 2, 9, 8, H, D, B, 0, E, 3, F, C, A, G]
, , ,2, ,0,2,9,8,H,D,B,0,E,3,F,C,A,G,
, , , ,0,0,2,2,3,8,9,A,B,C,D,E,F,G,H,

[H, G, D, E, F, 0, A, 9, C, B,  ,  , 0, 2, 3, ,  , 8, 2]

H,G,D,E,F,0,A,9,C,B, , ,0,2,3,, ,8,2,

H,G,F,E,D,C,B,A,9,8,3,2,2,0,0, , , ,,

[[s23 17], , [s11 18], , [s49 28], , [s35 135], , [s83 32], , [s99 51], , [s56 68], , [s34 160], , [s36 190], , [s85 35], , [s2 81], , [s15 78], , [s54 186], , [s6 126], , [s62 155], , [s32 491], , [s16 190], , [s74 316], , [s78 307], , [s81 180], , [s8 37], , [s91 131], , [s95 85], , [s26 205], , [s50 297], , [s13 391], , [s27 228], , [s28 481], , [s58 385], , [s61 721], , [s63 573], , [s64 585], , [s33 624], , [s68 356], , [s9 205], , [s73 399], , [s4 366], , [s76 713], , [s19 498], , [s20 234], , [s43 200], , [s41 220], , [s87 129], , [s45 638], , [s89 215], , [s92 611], , [s22 241], , [s25 557], , [s96 484], , [s100 397], , [s51 624], , [s52 462], , [s53 467], , [s3 288], , [s55 542], , [s14 989], , [s57 720], , [s29 705], , [s59 611], , [s60 990], , [s30 961], , [s31 920], , [s7 705], , [s1 938], , [s65 833], , [s66 833], , [s67 862], , [s17 996], , [s69 760], , [s70 230], , [s71 559], , [s72 992], , [s18 668], , [s37 406], , [s75 505], , [s38 886], , [s77 737], , [s39 549], , [s79 582], , [s10 842], , [s80 332], , [s40 708], , [s82 429], , [s84 934], , [s21 800], , [s86 583], , [s42 367], , [s88 983], , [s5 832], , [s44 650], , [s90 356], , [s46 774], , [s93 648], , [s94 766], , [s47 329], , [s48 899], , [s97 690], , [s24 842], , [s98 603], , [s12 830], ]

[s23 17], [s11 18], [s49 28], [s83 32], [s85 35], [s8 37], [s99 51], [s56 68], [s15 78], [s2 81], [s95 85], [s6 126], [s87 129], [s91 131], [s35 135], [s62 155], [s34 160], [s81 180], [s54 186], [s16 190], [s36 190], [s43 200], [s9 205], [s26 205], [s89 215], [s41 220], [s27 228], [s70 230], [s20 234], [s22 241], [s3 288], [s50 297], [s78 307], [s74 316], [s47 329], [s80 332], [s68 356], [s90 356], [s4 366], [s42 367], [s58 385], [s13 391], [s100 397], [s73 399], [s37 406], [s82 429], [s52 462], [s53 467], [s28 481], [s96 484], [s32 491], [s19 498], [s75 505], [s55 542], [s39 549], [s25 557], [s71 559], [s63 573], [s79 582], [s86 583], [s64 585], [s98 603], [s92 611], [s59 611], [s33 624], [s51 624], [s45 638], [s93 648], [s44 650], [s18 668], [s97 690], [s29 705], [s7 705], [s40 708], [s76 713], [s57 720], [s61 721], [s77 737], [s69 760], [s94 766], [s46 774], [s21 800], [s12 830], [s5 832], [s66 833], [s65 833], [s24 842], [s10 842], [s67 862], [s38 886], [s48 899], [s31 920], [s84 934], [s1 938], [s30 961], [s88 983], [s14 989], [s60 990], [s72 992], [s17 996],

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：77250次
• 积分：1336
• 等级：
• 排名：千里之外
• 原创：43篇
• 转载：89篇
• 译文：0篇
• 评论：1条
阅读排行
评论排行
最新评论