Suppose I have n number of strings, now I want to map each string to an integer within range from 0 to n-1 using a function such that whenever I call a function and pass the string and the n it will give me same and unique mapping on the go. So suppose if I have 4 strings "str1","str2","str3","str4" then the mapping will be from 0-3 and unique.
I tried doing something like : str.hashCode() % n, this is giving me the same mapping but is not within the range of 0 to n-1. I found something in PHP which is similar to this here-
https://madcoda.com/2014/04/how-to-hash-a-string-to-integer-with-a-range-php/
str.hashCode() % nshould be exactly what you want.Math.abs(str.hashCode() % n)is what you want.int