r/javahelp • u/Correct_Classroom • 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?
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
Mar 15 '20
You do realize that Java has a build-in hashmap class?
1
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.