首页 > 其他 > 详细

java中(优先队列)PriorityQueue的使用

时间:2014-02-21 11:43:59      阅读:588      评论:0      收藏:0      [点我收藏+]
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);
		
		
		
		
	}

}

结果:

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 


java中(优先队列)PriorityQueue的使用

原文:http://blog.csdn.net/zhu_9527/article/details/19565283

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!