This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Regarding hashcode method for String order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding hashcode method for String order" Watch "Regarding hashcode method for String order" New topic
Author

Regarding hashcode method for String order

Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
Hi All,

Could anyone please let me know how to generate hashcode for string based on the their alphabetical order?

I have checked about generating hashcode of string but its based on direct method of generating gethashcode method.

Could anyone please let me know how to override hashcode method to get the above requirement fulfilled.

I think i might need to use combination of hashcode and equals method.

Could anyone please help me with some kind of hint in this regard?

Thanks in Advance.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You can't override String's hashCode because you can't extend String; it's a final class.

What are you trying to accomplish, and what do you even mean by generating a hashcode "based on alphabetic order"? If you want to do it for sorting, then, no, because 1) That's the wrong approach, and 2) It's impossible, since there are more possible Strings than possible hashCode values.

If you want to sort a bunch of Strings, just put them into a List and call Collections.sort(). You can google for something like java sorting tutorial for more info if needed.

Otherwise, please try to explain what you're actually trying to accomplish.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
Jeff is right. You are confusing hash codes with compare to or compare methods. As for the tutorial, try here.
Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
Hi Jeff / Campbell.

Thanks for reply. I want to override hashcode() method of collection and want to assign as custom hashcode value to a string based on their alphabetical order.

The Requirement is -

I have a collection of object and need to sort them based on the Fullname (String value) property of each object. I would like to achieve by overriding hashcode() method of collection.
Because objects are collection of vector.

Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jay Shukla wrote:Hi Jeff / Campbell.

Thanks for reply. I want to override hashcode() method of collection


Why? I asked you what you're trying to accomplish, but that tells me how you're trying to accomplish it. Whatever you're trying to do, overriding the hashCode() method of a collection is almost certainly not the way to do it.

If you override the hashCode() method of a collection, that means that it will affect how that entire collection gets stored in a hash-based data structure. So, for instance, if your collection is the key in a HashMap, then the collection's hashCode() determines in which bucket the map will put the entry that has your collection as a key.

I don't think this is what you want to do.

and want to assign as custom hashcode value to a string based on their alphabetical order.


This makes no sense. Once again: Hashcode has nothing to do with ordering. A "hashCode based on alphabetical ordering" would have to either A) use other Strings in the group to determine its value, or B) be unique for all Strings.

A) is wrong, because a hashCode should depend only on the object's own internal state, not on how it relates to other objects.

B) is impossible, unless you can put very strict limitations on the set of Strings you're dealing with. For instance, even if your string is allowed to only use the capital letter 'A', there are still as many possible strings as there are possible hashCodes. You'll use up all your hashCodes with only a single letter.


The Requirement is -

I have a collection of object and need to sort them based on the Fullname (String value) property of each object.


That's what we needed to hear. And you've already been given tips to get you started. I'm not going to repeat them. Go back and read the previous responses more closely, please. After you spend some time researching and trying to work it out, if you get stuck, come back and as a specific question about the specific bits that are giving you trouble.

I would like to achieve by overriding hashcode() method of collection.


That's the wrong approach because, to summarize the above.

1. It's not the collection's hashCode that would matter, it's the hashCode of the elements it contains.

2. You can't override String's hashCode.

3. That's an inappropriate use of hashCode anyway.

4. It's impossible.
 
jQuery in Action, 2nd edition
 
subject: Regarding hashcode method for String order
 
Similar Threads
hashCode() Method
compareTo method doubt
Confusion on Hashcode and equal method
Bi-directional many-to-many mapping question
Strings