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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.