자바 Priority Queue 의 사용법

6539 단어 Java
우선 순위 대기 열 PriorityQueue
대기 열 에 있 는 toString 과 Iteator 를 통 해 대기 열 에 있 는 요 소 를 방문 하면 대기 열 순서 가 아 닌 저장 순 서 를 얻 을 수 있 습 니 다.
대기 열의 정렬 방식(poll)을 사용 하여 얻 은 요 소 는 정렬 순서 입 니 다.
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 intQueue = new PriorityQueue(intList);
	    System.out.println(intQueue); //     [0, 0, 3, 1, 1, 3, 6, 9, 2, 4]
	    Iterator 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());
	    intQueue.addAll(intList);
	    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 strQueue = new PriorityQueue(Arrays.asList(s.split("")));
	    System.out.println(strQueue);  // [,  ,  , 2,  , 0, 2, 9, 8, H, D, B, 0, E, 3, F, C, A, G]
	    Iterator 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());
	    strQueue.addAll(Arrays.asList(s.split("")));
	    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 queue = new PriorityQueue(11,
		        new Comparator() 
		        {
		          public int compare(Student s1, Student s2) {
		            return s1.grade - s2.grade;
		          }
		        }
		);	    
			
		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;
	int grade;
	public Student(String name, int grade)
	{
		this.name = name;
	    this.grade = grade;
	}	
	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],

좋은 웹페이지 즐겨찾기