- import java.util.*;
- class PQ{
- static class PQsort implements Comparator<Integer>{ // inverse sort
- public int compare(Integer one, Integer two){
- return two - one; // unboxing
- }
- }
- public static void main(String[] args){
- int[] ia = {1,5,3,7,6,9,8}; // unordered data
- PriorityQueue<Integer> pq1 =
- new PriorityQueue<Integer>(); // use natural order
- for (int x : ia) // load queue
- pq1.offer(x);
- for (int x : ia) // review queue
- System.out.print(pq1.poll() + " ");
- System.out.println("");
- PQsort pqs = new PQsort(); // get a Comparator
- PriorityQueue<Integer> pq2 =
- new PriorityQueue<Integer>(10,pqs); // use Comparator
- for (int x : ia) // load queue
- pq2.offer(x);
- for (int x : ia) // review queue
- System.out.print(pq2.poll() + " ");
- System.out.println(" ");
- }
- }
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Friday, April 11, 2014
How Priority Queue in Java is Working
Source Code
Tuesday, March 5, 2013
Installing and Removing Netbeans 7.3 in Ubuntu Via PPA
Installing and Removing Netbeans 7.3 in Ubuntu Via PPA
Subscribe to:
Posts (Atom)