• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dont know how to compare objects with compareTo() method.  RSS feed

 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following method stubs to complete:



I do not understand how to complete the compareTo() method and the equals() method. I understand that the compareTo() to should return 1 if the argument is smaller -1 if its bigger and 0 if they are the same, but I do not understand how it's possible to compare the current object with the object passed in as the parameter.

This is what I tried for the compareTo():



But my compiler tells me its a recursive call so I am not sure if I am doing this correctly.

Basically I am supposed to compare an object to another but I am not sure how to access the object that is not passed in as a parameter and compare it. Same goes for the equals(). This is what I tried for that (compiler also says its a recursive call):




 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ivanovich wrote:
I do not understand how to complete the compareTo() method and the equals() method. I understand that the compareTo() to should return 1 if the argument is smaller -1 if its bigger and 0 if they are the same, but I do not understand how it's possible to compare the current object with the object passed in as the parameter.

This is what I tried for the compareTo():



But my compiler tells me its a recursive call so I am not sure if I am doing this correctly.

Basically I am supposed to compare an object to another but I am not sure how to access the object that is not passed in as a parameter and compare it. Same goes for the equals(). This is what I tried for that (compiler also says its a recursive call):





Well, basically, it is *your* item. The compiler doesn't magically understand how to compare them, or how to check if they are equal. Only your program knows how how to do that -- which is based on business logic.

So, what is the business logic for comparison? You have access to the ID and INV variables -- do checking them give you an idea of equality? Or if one is greater than the other?

Henry
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ivanovich wrote:I understand that the compareTo() to should return 1 if the argument is smaller -1 if its bigger and 0 if they are the same...

That is not exactly right. I mean, if you write your compareTo method like that it will work fine.
What I mean is that you don't have to.

Javadoc for Comparable#compareTo() says:
int compareTo(T o)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

It doesn't have to be exactly -1 or 1.

One more thing. If you write compareTo() method inside Item class you'll probably need to declare Item as
public class Item implements Comparable<Item>

Another thing. Do not declare your equals method as
public boolean equals(Item other){ }
That is overloading.

What you need is to override a method from Object class.

So the method declaration should be:
public boolean equals(Object other){ }

...or even better
@Override public boolean equals(Object other){ }

That way, you'll not produce overloaded method by mistake.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equals method can access private members directly, so in its simplest form it could be something like:


If such an object has two fields that determine its state, then it's not clear that it makes sense for it to implement Comparable - I can imagine situations where you would want to sort a collection of such objects by ID, and I can imagine situations where you would want to sort by inventory (if that's what it is). Does either of these feel "natural" to you, as described in the javadocs of the Comparable interface?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:The equals method can access private members directly, so in its simplest form it could be something like:

Nope, it won't work. You need to cast other to Item (Object class doesn't have myId nor myInv fields). Also this method does not obey contract of Object#equals(). Assume you cast other to Item (and changed nothing else in your method).
The equals metod can't throw ClassCastException or NullPointerException (and your method would do that in some cases).
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hence "something like"
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!