How can I check if a HashSet contains an item using the hash of the item?

后端 未结 2 1421
天命终不由人
天命终不由人 2021-01-21 04:30

I want to check if a HashSet contains a specific element. I already have the int hashcode of the element but not a reference to the element itself.

Is it po

2条回答
  •  生来不讨喜
    2021-01-21 05:19

    No. Hash functions are one-way functions. It maps instances from a (potentially infinity large) space to a limited large space. It speeds up process, but doesn't guarantee uniqueness.

    A very simply hash function in daily life can be: to store employee records, the secretary uses 26 folders, named 'A' to 'Z'. Juliet's record is stored in folder 'J', Mike's record in 'M', Charlie's in 'C'. Now I give you these 26 folders, and you want to know whether there is someone called Oscar in the company. Unless the 'O' folder is empty, you'll have to check every record in that folder to tell the answer.

提交回复
热议问题