r/javahelp • • Mar 15 '20

Hashmap

What is the simplest hashmap you can code? I'm thinking of an int[] array with linkedlist at each index? Is there any other way? also please provide code, I'm stuggling to code one perfectly?

0 Upvotes

7 comments sorted by

1

u/BS_in_BS Extreme Concrete Code Factorylet Mar 15 '20

The simplest you can code is to use a single array, and use the next free space in the table to store an item on collision. Note this make deleting items tricky.

1

u/Correct_Classroom Mar 15 '20

I'm not taking the case of collision right now..it case of collision it just appends to the last index. So basically with separate chaining

1

u/lookForProject Mar 15 '20

I never build one, but my first guess/try would be: two arrays. One for key, one for value. The value is the bucket. Most simplest solution would be making bucket that's just an array.

1

u/RhoOfFeh Mar 15 '20

I would take my cue from the standard library and create an object to represent a single entry in the Map. An array of these will serve pretty well I suppose. An ArrayList would simplify things but I'm not sure whether you are seeking to avoid the Collections related standards altogether.

-1

u/[deleted] Mar 15 '20

You do realize that Java has a build-in hashmap class?