I have hashmap that has keys like this:’2+4+5′ , ‘653+65+1324+75’.(integer values delimited by a + sign)
What could be a good hashcode and equals method so that keys like ‘2+4+5’, ‘5+4+2’, ‘4+5+2’ … (all the permutation of 2,4,5) should return the same hashcode value and equals should return true.
I am planning to take the integer values in the key, sort them, put them in an ascending order into a string and call that strings hashcode and equals method. Suppose if I have ‘5+2+4’ then I would change that to “245” and call the strings hashcode and equals method. But this is going to be an expensive operation since each time I have to do sorting. And all the methods in the hashmap like put,get… would again be expensive
Are there any other ways of doing this in a log or linear time…
Test code: