It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Meaningfully equal in equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Meaningfully equal in equals()" Watch "Meaningfully equal in equals()" New topic
Author

Meaningfully equal in equals()

pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
my question is what does meaningfully equal covers?
as far as i understand it means to check whether the two objects have the same value for the instance variable for which they are compared.
but when i compare two string objects with values"pradeepta chopra" and "Pradeepta chopra", it evaluates to false.
why doesnt it ignore the case considerations while comparing two string objects??
i mean "pradeepta chopra" and "Pradeepta chopra" are meaningfully equivalent
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Originally posted by pradeepta chopra:
i mean "pradeepta chopra" and "Pradeepta chopra" are meaningfully equivalent

Regardless of that specious claim, the values are not the same and it's just silly to think that they should be considered so.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
Generally we use strings to represent names or address where capitalization does not seem to affect the value actually.
would'nt it be better if the case considerations were ignored??
our actual aim is to find oyt whether the two strings are meaningfully equivalent or not
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Strings can be used for all manner of things. If you want a data value in which case is not significant, you can easily create your own abstraction in which equals() acts in a manner suitable for the abstraction. To assert that String should be case-insensitive because a small handful of the types of data it can be used to present can be construed to be equivalent disregarding case would be short-sighted indeed!
pradeepta chopra
Ranch Hand

Joined: Jul 05, 2008
Posts: 137
yupp thats right
thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

String has a method called equalsIgnoreCase (and also compareToIgnoreCase) in case you need those for your own equals() method.

Which makes me think: why isn't there a hashCodeIgnoreCase? When I use a case insensitive equality check in my equals method, and I also want to include the String's hash code in my own hashCode method, I have to convert the String to upper- or lowercase first, then use its hashCode method. I don't like that since it can create a new object - just so I can get a case insensitive hash code.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Which makes me think: why isn't there a hashCodeIgnoreCase? When I use a case insensitive equality check in my equals method, and I also want to include the String's hash code in my own hashCode method, I have to convert the String to upper- or lowercase first, then use its hashCode method. I don't like that since it can create a new object - just so I can get a case insensitive hash code.


I guess the reason is the the hashCode method is primarily used inside collections (HashSet, HashMap, etc), and all these classes would have to be aware of the hashCodeIgnoreCase() method. The required type checks, in turn, would make the collections slower.

However, to take your idea further, it would also be helpful if collections that are (via generics) specified to contain Strings would offer a Set<String>.containsIgnoreCase(), Map<String,Object>.getIgnoreCase() methods, etc. But this won't be possible too soon, I'm afraid ...

If you need case insensitivity in a larger degree, one solution would be to create a CaseInsensitiveString class.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

But the HashSet / HashMap should not care about this method - they can just keep on using equals and hashCode. The hashCodeIgnoreCase method would be just for the cases where you determine equality using equalsIgnoreCase, and also want those fields in the hashCode implementation.

I'm now instead planning on creating a helper method that does this for me. I can't imagine why I haven't thought of this earlier - it sure beats calling toLowercase or toUppercase each time.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
I was just wondering where one might use hashCode() in non-JRE code on a regular basis ... could you provide an example?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Not me. The only places I call hashCode on an object is in my own custom hashCode methods.
 
wood burning stoves
 
subject: Meaningfully equal in equals()