0

Hi all,

I have two strings, in which the order of words may differ. I would like to hash both of them and obtain the very same hash code for them. So I need a hash that returns the same value for "Hello world" and " world Hello". Does such a hashing strategy exist?

Thank you,
Chaster

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by javaAddict
0

You can use the split method to create an array of the words of the String.
Take that array and sort it.
Then create a new String from that array. So now you have a new String with the words of the previous one sorted. Now you can use the equals method.

String s1 = "b a c";
String [] tok1 = s1.split(" ");
Arrays.sort(tok1);
for - loop the tok1 to create a new String: "a b c"

String s2 = "c b a";
// do the same and the final String would be: "a b c"

// Now you can compare them
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.