We need to access same data from concurrent hash map using two different keys (say using user id OR using user name).
Currently we are achieving it by inserting data twice, each with different key. It is working, but is there a better way?
If the both statement are negative than you can insert structures of <id, name> and define your own HashCompare that implements equal with the following behavior:
Where X any value, 0 is impossible value.
Than if you need to find something by id, find <id,0>. If by name, find <0, name>.
Thanks for the suggestion. Each user has an unique id and a name.
However, what about hash() function? the hash for <id, name> will be different from <id, 0> or <0, name> hash and in this case equal() will not be called at all. Pls correct me if wrong but I assume equal() only gets called for hash collisions (same hash).
You are right. I though about iterating over the container but it is not concurrent and it will be a linear search (that is not what is expected). Looks like you really need two maps.