• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Priority Queue

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am having hard time with this code from K&B..Could you please let me know ? If Line#6 output is 2, Line #12 output also should be 2 know ? Also, What is the difference between offer() and add() ??..I see both of them are adding to the Priority Queue
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As you are adding Strings to the priority queue- The comparison according to me would go by the ascii value of the character (natural ordering) and not by the actual value of the number. And the priority would be based on the Ascii right? You could provide Comparator to give the custom ordering.

You could try adding Integers to the Queue and see the difference.

peek() - checks which value can be retrieved.
offer()- Inserts the value according to the priority and add() - might just add the value (at the end?) Am not too sure about this. But looking at the API I thought this might be the difference.

API:
boolean add(E o) - Adds the specified element to this queue.
boolean offer(E o)- Inserts the specified element into this priority queue
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no difference between offer and poll. From the PriorityQueue source



As for the comparisons, you are comparing Strings not integers. "12" is smaller than "5" because it starts with a "1".
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:There is no difference between offer and poll. From the PriorityQueue source




Oh yeah, the JavaSE 6 Documentation has the same description for both

I had a look at the JavaSE 5 Documentation- which used- queue for add and priority queue for offer(). But is it not strange that both of them do the same operation and still exist together in the API
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But is it not strange that both of them do the same operation and still exist together in the API



Yes it is. I do not have a JDK 5 source at the moment to dig in and see if the javadoc alone has changed or the impl has as well. But it is an interesting question
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:
What is the difference between offer() and add()?





Both add() and offer() insert element in the queue, and return value tells the success or failure of the opertion.
The difference is that add() method is inherited from the Collection interface and throws IllegalStateException if queue is full.
On the other had, offer() method doesn't.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The difference is that add() method is inherited from the Collection interface and throws IllegalStateException if queue is full.



How did you come to this conclusion ? A PriorityQueue grows in size as more elements are added. It is never really 'full'.


add

public boolean add(E o)

Adds the specified element to this queue.

Specified by:
add in interface Collection<E>
Overrides:
add in class AbstractQueue<E>

Parameters:
o - the element
Returns:
true (as per the general contract of Collection.add).
Throws:
NullPointerException - if the specified element is null.
ClassCastException - if the specified element cannot be compared with elements currently in the priority queue according to the priority queue's ordering.


 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:
How did you come to this conclusion ?



It's given in Khalid Mughal.
May be by "full", it means there's no more memory available in JVM to add items.
I haven't checked Java docs regarding this, which should be the ultimate reference regarding Java.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

May be by "full", it means there's no more memory available in JVM to add items.



That would result in an OutOfMemoryError, not an IllegalStateException. The javadocs do not claim that an IllegalStateException is thrown when the queue is full
 
reply
    Bookmark Topic Watch Topic
  • New Topic