Re: Hashtables
- From: Kenneth Patrick Turvey <kt-usenet@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 12 Sep 2005 14:36:12 -0500
On Mon, 12 Sep 2005 20:10:32 +0100, Thomas Hawtin wrote:
> JTMOBAP wrote:
>> For any given hashtable, what is the maximum number of collisions in
>> buckets?
>
> The number of entries in the table. Why do you ask?
OK, to be a bit more helpful...
Each hashtable determines what bucket to put an Object in by executing the
method Object.hashCode() or a method that overrides it. This method
returns an integer, but there is no reason at all that it couldn't return
exactly the same integer for everything you put in the hashtable. The
contract for hashCode() only requires that for two Objects that are
equal() it return the same value.
In this case every Object added to the hashtable would end up in the same
bucket.
This isn't the only case in which this happens though.
Every Hashtable has a capacity. This is the number of buckets in the
Hashtable. This capacity is really never the size of
Integer.MAX_VALUE, so some mapping must be made from the hashCode() of
your object to the location in the Hashtable. So maybe all your Objects
just happen to end up in the same bucket.
The more objects in the table and the better the implementation of
hashCode the less likely this is.
--
Kenneth P. Turvey <kt-usenet@xxxxxxxxxxxxxxxxxx>
http://kt.squeakydolphin.com (not much there yet)
Phone: (314) 255-2199
Jabber IM: kpturvey@xxxxxxxxxx
.
- References:
- Hashtables
- From: JTMOBAP
- Re: Hashtables
- From: Thomas Hawtin
- Hashtables
- Prev by Date: Re: help converting project into eclipse 'java' project
- Next by Date: Artificial Intelligence
- Previous by thread: Re: Hashtables
- Next by thread: Re: Hashtables
- Index(es):
Relevant Pages
|