Results 1 to 5 of 5
Thread: PriorityQueue question
- 02-28-2011, 09:09 AM #1
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?
- 02-28-2011, 09:45 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 1
- Rep Power
- 0
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); }
- 02-28-2011, 09:57 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-28-2011, 02:11 PM #4
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.
- 02-28-2011, 02:20 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Pseudo-priorityQueue datastructure
By leeple in forum Advanced JavaReplies: 2Last Post: 03-01-2011, 11:30 AM -
PriorityQueue question
By j2me64 in forum Advanced JavaReplies: 0Last Post: 02-28-2011, 09:00 AM -
Question concerning question marks and colons
By jim01 in forum New To JavaReplies: 17Last Post: 01-14-2011, 12:05 AM -
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
JSP Question
By maheshkumarjava in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-29-2008, 10:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks