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.
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.
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.
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.