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

  1. import java.util.*;  
  2. class PQ{  
  3.     static class PQsort implements Comparator<Integer>{   // inverse sort  
  4.         public int compare(Integer one, Integer two){  
  5.             return two - one;           // unboxing  
  6.         }  
  7.     }  
  8.     public static void main(String[] args){  
  9.         int[] ia = {1,5,3,7,6,9,8};         // unordered data  
  10.         PriorityQueue<Integer> pq1 =   
  11.             new PriorityQueue<Integer>();     // use natural order  
  12.           
  13.         for (int x : ia)                // load queue  
  14.             pq1.offer(x);  
  15.               
  16.         for (int x : ia)                // review queue  
  17.             System.out.print(pq1.poll() + " ");  
  18.         System.out.println("");  
  19.               
  20.         PQsort pqs = new PQsort();          // get a Comparator  
  21.         PriorityQueue<Integer> pq2 =   
  22.             new PriorityQueue<Integer>(10,pqs);   // use Comparator  
  23.               
  24.         for (int x : ia)                // load queue  
  25.             pq2.offer(x);  
  26.           
  27.           
  28.         for (int x : ia)                // review queue  
  29.             System.out.print(pq2.poll() + " ");  
  30.           
  31.         System.out.println(" ");  
  32.     }  

Tuesday, March 5, 2013

Installing and Removing Netbeans 7.3 in Ubuntu Via PPA

 Installing and Removing Netbeans 7.3 in Ubuntu Via PPA