aspose file tools*
The moose likes Beginning Java and the fly likes PriorityQueue ,add and compare object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "PriorityQueue ,add and compare object" Watch "PriorityQueue ,add and compare object" New topic
Author

PriorityQueue ,add and compare object

Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Hi ,I have prblem with inserting objcet in PriorityQueue,and putting them in order.When I insert the first element everithing is fine,but when I try the second I have errors
Thanks.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Don’t call your classes Object.
We can’t help if you don’t tell us what is going wrong. Please supply more details. Have you read the PriorityQueue documentation? It describes some very definite requirements.
Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Thanks
This is the error message.Can I specify by what member variable the object should be comapred

Exception in thread "main" java.lang.ClassCastException: Practice.Obj cannot be cast to java.lang.Comparable
at java.util.PriorityQueue.siftUpComparable(Unknown Source)
at java.util.PriorityQueue.siftUp(Unknown Source)
at java.util.PriorityQueue.offer(Unknown Source)
at java.util.PriorityQueue.add(Unknown Source)
at Practice.QueueExample.main(QueueExample.java:10)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Lio Liov wrote:Thanks
This is the error message.Can I specify by what member variable the object should be comapred

Exception in thread "main" java.lang.ClassCastException: Practice.Obj cannot be cast to java.lang.Comparable


And you can't even make a guess at what that might mean? Not even after reading that message carefully and closely, and reading PriorityQueue's docs carefully and closely?
Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Thanks for your time.
Apperenty I can not.
I am asking for help politely (not for sarcasm)
If you have the time and patient help me if not Dont waste your time.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Does it help if we tell you that when an object is added to a PriotityQueue, the queue compares it to the other elements it is holding to see if they need to be reordered? And that therefore every element in the queue must be mutually comparable with every other element? In Java, this statement implies something very specific about those elements.
Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Thanks,I think I got that
Can I compare the two object by only one object argument
This is what I did so far .Am I going in the right direction.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Lio Liov wrote:Thanks for your time.
Apperenty I can not.




So, do you know what ClassCastException is? Did you try looking at its javadocs, or googling for it?

Do you know what casting is? Did you try googling for it?

Do you know what Comparable is? Did you try looking at its javadocs or googling for it?

I am asking for help politely (not for sarcasm)


My post was intended to help (as is this one), and there was not one bit of sarcasm in it. I'm trying to get you to a) Realize that the compiler's error messages contain all the information you need to understand what the problem is and b) Learn to do some research before posting so you can become a more self-reliant.
Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Thank you for your time.
Maybe from the position of person knowing Java,it is hard for you to be on the other side(person tring to learn Java)
Of course first I am googling ,and try to read the docs .But sometimes is confusing!!!And then I am asking for help.
And on my code again
If I want to put this priority queue in order,I have to use comparable to point what argument to compare,and then what(do I have to do the swap)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Lio Liov wrote:Thank you for your time.
Maybe from the position of person knowing Java,it is hard for you to be on the other side(person tring to learn Java)
Of course first I am googling ,and try to read the docs .But sometimes is confusing!!!And then I am asking for help.


Okay, I guess this is going nowhere. What I was trying to get you do to was tell us what you read, and what part of it you didn't understand. The error message "X cannot be cast to Y" doesn't have a lot of complexity. I was trying to get you to explain what part of it you (on the other side, trying to learn Java) didn't understand. For instance, if I tell you, "You can't cast a Date to a List," do you know what that means? If not, what part don't you get?

I'm definitely not expecting you to know everything. I'm only expecting you to show some effort and communicate the details of your problem.


And on my code again
If I want to put this priority queue in order,I have to use comparable to point what argument to compare,and then what


It's not really clear what you're asking here. The idea behind Comparable is that it's a way for other classes--classes that put things in order based on "less", "equal", or "greater"--to find out which of two objects of your class is "less than", "equal to", or "greater than" the other. So if I call youClassObject1.compareTo(yourClassObject2), your compareTo method will return negative, zero, or positive, telling me that obj1 is less than, equal to, or greater than obj2, which I can then use decide whether to leave them as they are or swap them.

So, if you want your objects to be ordered correctly for a PriorityQueue, then you have to write a compareTo() method that looks at the priority of this and the priority of the object passed in, and deicdes which one is "less".

(do I have to do the swap)


No. You don't call compareTo(), and you don't do the swap. The PQ calls compareTo() and uses the results to decide which object to put in front of which.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Lio Liov wrote:This is what I did so far .Am I going in the right direction.

Yes, you are.
do I have to do the swap

No, the queue will perform this operation. All it needs is your compareTo method so it knows the desired order.
Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Thanks
In my Obj.java(the code could be seen above) I created this method CompareTo ,and I am comparing the integer number in the object
In my QueueExample class after I create and add all this object,Do you have to run a loop (to compare the ojbects and put them in the right position)
You said that the queue will perform the operation,Could you show me with code how can I do that
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18986
    
    8

Lio Liov wrote:Do you have to run a loop (to compare the ojbects and put them in the right position)
You said that the queue will perform the operation,Could you show me with code how can I do that


No. The queue will compare the objects as you add them, and keep them in the right order. To repeat: The queue will do it. You don't have to compare the objects, and there is no point anyway because the queue will do that. All you have to do is to have your object implement Comparable, so that the queue can do its job. And it looks like you have done that.
Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Thanks
Still problem with the priority queue.Compilation error
How do you pass the object to compare and the argument to compare to the priority Queue




Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Lio Liov wrote:
Still problem with the priority queue.Compilation error


Copy/paste the exact, complete error message, and indicate clearly which line is causing it.
Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Apperenty I don know how to connect comparator with priority queue
The error is in QueueExample class the line with expanation mark comments
Thanks

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Obj cannot be resolved to a variable
NumberComparator cannot be resolved to a variable

at Practice.QueueExample.main(QueueExample.java:10)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Lio Liov wrote:Apperenty I don know how to connect comparator with priority queue
The error is in QueueExample class the line with expanation mark comments
Thanks

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Obj cannot be resolved to a variable
NumberComparator cannot be resolved to a variable

at Practice.QueueExample.main(QueueExample.java:10)



Obj is telling us what type of object the Comparator can compare and the Queue can store. When you use Obj.getNumber() it's trying to interpret Obj as a variable, but you don't have any variable named Obj. (Important: Do you understand the difference between a type and a variable?) I can't even guess what you thought Obj.getNumber() would do or mean.

Presumably you're trying to use this constructor: PriorityQueue(int initialCapacity, Comparator<? super E> comparator) . If so, then you just need to provide an int that you choose as the initial capacity for your Queue.
Lio Liov
Ranch Hand

Joined: Mar 21, 2012
Posts: 33
Thank you
I add this code for printing,and everithing works fine

Why the previous code was not printing the queue in the right order
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Lio Liov wrote:
Why the previous code was not printing the queue in the right order


When you call println(Object), it ends up calling that object's toString() method. In PQ's docs, under "Methods inherited from class java.util.AbstractCollection," we see a link for toString(), and when we click that, we find out that toString() uses the PQ's Iterator.

At the top of the PQ docs, where it gives the desription, we see:
The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).


So, it's all there in the docs, although it can be a bit tricky to track it all down, especially if you're not aware of the starting point that println() calls toString().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PriorityQueue ,add and compare object