I have a String array that contains a lot of words. I wish to get the index of a word contained in the array (-1 if it is not contained).
I have first made a loop to search through all elements in the array while incrementing a variable and when I find it, I return the variable's value.
However the array can be very very very big so searching through all elements is extremely slow. I have decided that before adding a new word in my string array, I would use hashCode() % arrayLength to get the index of where I should put it. Then, to get the index back, I would just reuse hashCode() % arrayLength to instantly know at what index it is.
The problem is that sometimes there are "clashes", and two elements can have the same index in the array.
Anyone has an idea how to deal with it? Or any other alternatives to get the index of an element faster?
HashMap?HashMap<Integer, Object>where the key is the hash code.