Results 1 to 5 of 5
  1. #1
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default PriorityQueue question

    it looks like a lot of code but the code is very simple. i got a class Person

    Java Code:
    package priorityqueue2;
    
    public class Person {
    	private String name;
    	
    	public Person(String n) {
    		this.name = n;
    	}
    	
    	public String getName() {
    		return this.name;
    	}
    	
    	public String toString() {
    		return this.name;
    	}
    }

    and a class that implements the Comparator for the Person

    Java Code:
    package priorityqueue2;
    
    import java.util.Comparator;
    
    public class NameComparator implements Comparator {
    	public int compare(Object o1, Object o2) {
    		Person p1 = (Person)o1;
    		Person p2 = (Person)o2;
    		return p1.getName().compareTo(p2.getName());
    	}
    }

    and i'm testing the PriorityQueue with the following code

    Java Code:
    package priorityqueue2;
    
    import java.util.Iterator;
    import java.util.PriorityQueue;
    
    public class PriorityQueueExample {
    	public static void main(String[] args) {
    		PriorityQueue<Person> persons = new PriorityQueue<Person>(5, new NameComparator());
    		Person p3 = new Person("Noah");
    		persons.add(p3);
    		Person p1 = new Person("Digobert");
    		persons.add(p1);
    		Person p2 = new Person("Dagobert");
    		persons.add(p2);
    		
    		Iterator<Person> itr = persons.iterator();
    		while (itr.hasNext()) {
    			System.out.println(itr.next());
    		}
    	}
    }

    when i run the code the result is

    Dagobert
    Noah
    Digobert


    when i instantiate the priorityqueue i'm telling to use the NameComparator for sorting. so i expect that Digobert is before Noah. can somebody give an explanation about the order in the output?

  2. #2
    Tr1aL is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default

    Java Code:
            PriorityQueue<Person> persons = new PriorityQueue<Person>(5, new NameComparator());
            Person p3 = new Person("Noah");
            persons.add(p3);
            Person p1 = new Person("Digobert");
            persons.add(p1);
            Person p2 = new Person("Dagobert");
            persons.add(p2);
    
    
            while (persons.size() != 0) {
                Person p = persons.remove();
                System.out.println(p);
            }

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default

    The iterator for a priority queue iterates over all elements in the queue in a rando order (i.e. not the order in which the elements are logically ordered). See also the API documentation for the PriorityQueue class, it mentions a way to retrieve all elements in an ordered manner.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by JosAH View Post
    The iterator for a priority queue iterates over all elements in the queue in a rando order (i.e. not the order in which the elements are logically ordered). See also the API documentation for the PriorityQueue class, it mentions a way to retrieve all elements in an ordered manner.

    the api is your friend and says "The iterator does not return the elements in any particular order". so far, all clear about the iterator with priority queues. other question: is the method remove() the only way to get the elements in an ordered way?

    Looking a little closer to the api i found "If you need ordered traversal, consider using Arrays.sort(pq.toArray())".
    Last edited by j2me64; 02-28-2011 at 02:15 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    the api is your friend and says "The iterator does not return the elements in any particular order". so far, all clear about the iterator with priority queues. other question: is the method remove() the only way to get the elements in an ordered way?

    Looking a little closer to the api i found "If you need ordered traversal, consider using Arrays.sort(pq.toArray())".
    Yep, and since you're using a comparator in your PriorityQueue object you can do this:

    Java Code:
    Arrays.sort(pq.toArray(), pq.comparator());
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Pseudo-priorityQueue datastructure
    By leeple in forum Advanced Java
    Replies: 2
    Last Post: 03-01-2011, 11:30 AM
  2. PriorityQueue question
    By j2me64 in forum Advanced Java
    Replies: 0
    Last Post: 02-28-2011, 09:00 AM
  3. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  4. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  5. JSP Question
    By maheshkumarjava in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 10:51 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
  •