开发者

How stupid it is to use an integer value a key for an Hash Table?

开发者 https://www.devze.com 2022-12-21 17:13 出处:网络
I\'ll be needing to use Hash Tables wi开发者_如何学Goth different keys. One as string for the key and another as integer.

I'll be needing to use Hash Tables wi开发者_如何学Goth different keys. One as string for the key and another as integer.

As for the integer one, how stupid it is to run a hash function on the number to generate a key?

I mean, the numbers I'll be using as key for the Hash Table will always be different, there will be no duplicates at all. Isn't enough for me to use the mod operator to "truncate" the value below the hash table size?

Or there's something more to it?


It is fine unless there's a high chance that your integer keys are 62, 93, 124, ... and your hash table size happens to be 31.

See What integer hash function are good that accepts an integer hash key? if you worry about this.


As with so many design questions in our field, the answer is: "It depends." There are certain, specific cases where it would be stupid to bother running a typical hash algorithm on the integers. If you know based on your specific situation that a modulus would distribute the expected data evenly, and if performance is very important to you, and if you'll need to access this hashtable quite a lot, then it would be stupid. Barring these conditions, there are a number of very good reasons to just use a generic hashing algorithm that will work well across a variety of circumstances. In the vast majority of cases, it would be stupid to do otherwise. In some cases, using a hashtable would be a stupid choice in the first place.

If you gave us more information about the type of data you're storing, why you're storing it, and how important performance is to you, we might be able to point you to a solution that works better than using a hashtable. Frameworks like Java and .NET have hash functions that are fast and avoid hashing numbers to the same bucket. In most cases, I'd trust the default hash method.


It's not stupid, it is perfectly sensible. An integer is a natural seed in a unique naming scheme. Allthough the relational zealot in me dies a little when I say things like this =D


In my opinion it's not stupid. It may not be the best option if you tend to have relatively few values (in which case using a plain array could be better).

I'd use the modulo operator to hash integers to the hash size.


what about in case of integer to use sorted array and do binary search? actually same for string, but for string hashing may be cheaper

0

精彩评论

暂无评论...
验证码 换一张
取 消