• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

PriorityQueue

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

Two questions -

Firstly can anyone explain why the following code fails? It compiles OK but when I run it, it fails on the second attempt to add something with...

Exception in thread "main" java.lang.ClassCastException: CD
at java.util.PriorityQueue.fixUp(Unknown Source)
at java.util.PriorityQueue.offer(Unknown Source)
at TestCD.main(CD3.java:54)



If I create the PriorityQueue using a Comparator then it works okay. Is it something to do with the PriorityQueue being unable to perform any ordering on the set because there is no defined "natural order"?

My other question is - on a PriorityQueue - what is the difference between add() and offer() - are they interchangeable?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to the first question is that when you do not create the PriorityQueue with a Comparator, the elements that you add to the queue must be mutually Comparable.

This means that you need to modify your class definition to implement Comparable.

The answer to the second question is from the API docs for the Queue interface.

offer

boolean offer(E o)

Inserts the specified element into this queue, if possible. When using queues that may impose insertion restrictions (for example capacity bounds), method offer is generally preferable to method Collection.add(E), which can fail to insert an element only by throwing an exception.

Parameters:
o - the element to insert.
Returns:
true if it was possible to add the element to this queue, else false
 
David A. Scott
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course! I knew this!

It feels like every time I manage to remember something, I forget something else important....



Thanks Keith
 
David A. Scott
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also.... with regard to the second question - I really should learn to look in the API before I post here! Apologies.
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic