Results 1 to 4 of 4
  1. #1
    java_on_fire is offline Member
    Join Date
    Mar 2010
    Posts
    19
    Rep Power
    0

    Default PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();

    Java Code:
    package priorityqueue;
    import java.util.*;
    
    public class PQ {
    	
    	static class PQsort implements Comparator<Integer>{
    		public int compare(Integer one, Integer two) {
    			return two-one;
    		}
    	}
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		int[] ia = {1, 5, 3, 7, 6, 9, 8 };
    		
    		PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();
    		
    		for( int x : ia)
    			pq1.offer(x);
    				
    		System.out.println(pq1);
    		
    		for ( int x : ia)
    			System.out.print(pq1.poll());
    		
    		System.out.print("\n" + pq1);
    		
    		PQsort pqs = new PQsort();
    		
    		PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs);
    		
    		System.out.print("\n" + pq2);
    		for(int x : ia)
    			pq2.offer(x);
    		System.out.print("\n" + pq2 + "\n");
    		
    		for (int x : ia)
    			System.out.print(pq2.poll() + " ");
    	}
    }
    1. from running the code above, i understood that the sorting would be done when a method poll() is called but why is it sorting when poll() is called? isn't it too late to sort after it removed all elements from priorityqueue? it doesn't make sense!

    2. also how is the sorting done? somehow a call is being made from inside the poll() to siftDown then another call to siftDownUsingComparator. Please look at method above and advise how is it sorting!!

    3. the initial capacity in PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs); is set to 10 but it still works if i change it to 1, but if i change it to 0 i will get an exception! i guess that's just the initial capacity and i can put one in there, correct!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();

    Quote Originally Posted by java_on_fire View Post
    1. from running the code above, i understood that the sorting would be done when a method poll() is called but why is it sorting when poll() is called? isn't it too late to sort after it removed all elements from priorityqueue? it doesn't make sense!
    That's what this type of queue is for -- any order in, priority order out.


    2. also how is the sorting done? somehow a call is being made from inside the poll() to siftDown then another call to siftDownUsingComparator. Please look at method above and advise how is it sorting!!
    I'm not sure how it's done. To know for sure, we'd have to look at the source code -- which is available to all. Why not take a look at it and report back to us?


    3. the initial capacity in PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs); is set to 10 but it still works if i change it to 1, but if i change it to 0 i will get an exception! i guess that's just the initial capacity and i can put one in there, correct!
    The API will tell you what the initial capacity parameter represents and what to expect if you try to construct a queue with an initial capacity of < 1.

  3. #3
    java_on_fire is offline Member
    Join Date
    Mar 2010
    Posts
    19
    Rep Power
    0

    Default Re: PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();

    Quote Originally Posted by Fubarable View Post
    That's what this type of queue is for -- any order in, priority order out.
    I'm not sure how it's done. To know for sure, we'd have to look at the source code -- which is available to all. Why not take a look at it and report back to us?
    i did look at source code, but i couldn't understand it! very complicated code for a beginner!

    The API will tell you what the initial capacity parameter represents and what to expect if you try to construct a queue with an initial capacity of < 1.
    ok, i looked at source code, if you pass 0, the method will through an exception, i just wanted someone to confirm that i understood that the initial capacity is able to change to accommodate more.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();

    See the API page for this class, it has a lot of information which should answer quite a few of your questions:
    PriorityQueue (Java Platform SE 6)

Similar Threads

  1. Cache behavior on PriorityQueue with VanEmdeBoas layout
    By nunofaria in forum Advanced Java
    Replies: 0
    Last Post: 04-04-2011, 01:59 PM
  2. PriorityQueue question
    By kryptonian03 in forum New To Java
    Replies: 9
    Last Post: 03-10-2011, 03:44 AM
  3. Pseudo-priorityQueue datastructure
    By leeple in forum Advanced Java
    Replies: 2
    Last Post: 03-01-2011, 11:30 AM
  4. PriorityQueue question
    By j2me64 in forum Advanced Java
    Replies: 4
    Last Post: 02-28-2011, 02:20 PM
  5. PriorityQueue question
    By j2me64 in forum Advanced Java
    Replies: 0
    Last Post: 02-28-2011, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •