Creating a special hashmap data thingy

ok so im trying to create something along the lines of a HashMap except the keys are integers, (I'm not using an array because the integers a quite random)

so i created 2 simple classes. The IntMapEntry is an entry for this map, it contains only an object as the value and an integer as the key. The IntMap (clever name, i know…) is another object which contains a collection of IntMapEntries.



The trouble that i am having, is that while my class is much simpler than HashMap, for some reason it is much slower in every aspect.

I'll start with the constructor for now, because this really confuzes me.



my code for the IntMap constructor is

   public IntMap(int size){

      entries = new IntMapEntry[size];

   }

   





mean while for hashmap

it's

 public HashMap(int initialCapacity, float loadFactor)

250   {

251     if (initialCapacity < 0)

252       throw new IllegalArgumentException("Illegal Capacity: "

253                                          + initialCapacity);

254     if (! (loadFactor > 0)) // check for NaN too

255       throw new IllegalArgumentException("Illegal Load: " + loadFactor);

256

257     if (initialCapacity == 0)

258       initialCapacity = 1;

259     buckets = new HashEntry[initialCapacity];

260     this.loadFactor = loadFactor;

261     threshold = (int) (initialCapacity * loadFactor);

262   }





now IntMap takes 2,262,578 nanoseconds to construct while HashMap takes 39,112 nano seconds. Quite a difference, no?

So what am i missing? thanks

Why reinvent the wheel? For education, I suppose… but still… you could be making games instead :slight_smile:



Eg. http://www.google.com/search?q=IntRadkeHashMap

that was too fast,

heh your right i suppose, but it would still be nice to know why there are such extreme differenc ein time

ok i just tried out radke solution and that seems to be slower than using HashMap… i wonder what's going on.

edit: after doing some testing, the actual code in the constructor takes little time to execute compared to making the call. WEhat the hell is going on?

wich program outputs execution times in nanoseconds?

actually it was a test program that i made MrCoder, I found that the problem came from "Shared class data", and that hashmap had that advantage, once i give IntMap a warm up of a couple thousand trials it out performs  HashMap. Thanks anyway guys!

MrCoder said:

wich program outputs execution times in nanoseconds?

System.nanoTime() - introduced in JDK1.5