• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Meaningfully equal in equals()

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yupp thats right
thanks
 
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not me. The only places I call hashCode on an object is in my own custom hashCode methods.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic