• Post Reply Bookmark Topic Watch Topic
  • New Topic

priority queue  RSS feed

 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have heard on a new(as in new to Java) priority queue data structure for 1.5. Is this covered in the book? Also, does this new structure require that the elements implement Comparable for this to work? What happens when they do not?
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Kretzer:
I have heard on a new(as in new to Java) priority queue data structure for 1.5. Is this covered in the book?


As far as I see in the TOC of the book, I don't see any chapter that discusses about priority queue data structure in the book...

Actually priority queue can be implemented in our own by using the existing data structures... It's not that difficult, I guess... I still remember my second year university life, where I used to implement a lot of data structres such queues, stacks, link lists, priority queues, red-black tree and so and so in the Data Structures course...

Hope this helps...
 
Jason Kretzer
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I had to make my own in college, too, and it is not too hard to do in Java. Just curious about how it works...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PriorityQueue

Also, does this new structure require that the elements implement Comparable for this to work? What happens when they do not?

Either the elements must implement Comparable, or you must define a Comparator. If you don't define a Comparator and you add a non-Comparable element, you will probably get a ClassCastException. (The API is a bit vague abou this.) I just ran a test, and if you add just one non-Comparable object to a PriorityQueue with no Comparator, nothing happens - the object is added normally. But once you add a second non-Comparable object, ClassCastException is thrown.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!