<pre class="brush:java;" style="font-family: 'Courier New', monospace; margin-top: 0px; margin-bottom: 1em; width: 630px; overflow: auto; padding: 10px 0px; color: rgb(51, 51, 51); line-height: 28px; background-color: rgb(255, 255, 255);">import java.util.*;
public class test1 {
public static void PrintPr(Queue queue){
while(queue.peek()!=null){
System.out.print(queue.remove()+" ");
}
System.out.println();
}
public static void main(String[] args) {
PriorityQueue<integer> priorityQueue=new PriorityQueue<integer>();
//优先级队列存放整型数字
Random random=new Random(47);
for(int i=0;i<10;i++){
priorityQueue.offer(random.nextInt(i+10));
}
PrintPr(priorityQueue);
List<integer> ints=Arrays.asList(25,22,20,18,14,9,3,1,1,2,3,9,2,5,1,5,25,30,0,10);
priorityQueue.addAll(ints);
PrintPr(priorityQueue);
priorityQueue=new PriorityQueue<integer>(ints.size(),Collections.reverseOrder());
priorityQueue.addAll(ints);
PrintPr(priorityQueue);
//优先级队列存放字符
String fact="UDJKKDJL WSAPLMAD IUJSAA ATHSHJ";
List<string> list=Arrays.asList(fact.split(""));
PriorityQueue<string> priorityQueueStrings=new PriorityQueue<string>(list);
PrintPr(priorityQueueStrings);
priorityQueueStrings=new PriorityQueue<string>(list.size(),Collections.reverseOrder());
priorityQueueStrings.addAll(list);
PrintPr(priorityQueueStrings);
//set去重在排序
Set<character> set=new HashSet<character>();
for(char c:fact.toCharArray()){
set.add(c);
}
PriorityQueue<character> pQCharacters=new PriorityQueue<character>(set);
PrintPr(pQCharacters);
}
}</character></character></character></character></string></string></string></string></integer></integer></integer></integer>
结果:
0 1 1 1 1 1 3 5 8 14
0 1 1 1 2 2 3 3 5 5 9 9 10 14 18 20 22 25 25 30
30 25 25 22 20 18 14 10 9 9 5 5 3 3 2 2 1 1 1 0
A A A A A D D D H H I J J J J K K L L M P S S S T U U W
W U U T S S S P M L L K K J J J J I H H D D D A A A A A
A D H I J K L M P S T U W