Results 1 to 2 of 2
- 01-31-2013, 04:08 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 11
- Rep Power
- 0
Feedback on this implementation of a HashMap
So I have tried to implement a HashMap myself via a Node class, which just
constructs a node with a key and a corresponding value, it has also got a getHashCode method, where I just concatenate the two values on each other.
I have also constructed a SinglyLinkedList (part of a previous assignment), which I use as the bucket.
And my Hash function is simply hashCode % hashMap.length.
Here is my own implementation, so what do you think of it?
The look up time should be around O(1), so I would like to know if that is the case? And if not how can I improve it, in that regard?Java Code:package spreadsheet; public class HashTableMap { private SinglyLinkedListMap[] hashArray; private int size; public HashTableMap() { hashArray = new SinglyLinkedListMap[64]; size = 0; } public void insert(final Position key, final Expression value) { Node node = new Node(key, value); int hashNumber = node.getHashCode() % hashArray.length; SinglyLinkedListMap bucket = new SinglyLinkedListMap(); bucket.insert(key, value); if(hashArray[hashNumber] == null) { hashArray[hashNumber] = bucket; size++; } if(hashArray[hashNumber] != null) { SinglyLinkedListMap bucket2 = hashArray[hashNumber]; bucket2.insert(key, value); hashArray[hashNumber] = bucket2; size++; } if (hashArray.length == size) { SinglyLinkedListMap[] newhashArray = new SinglyLinkedListMap[size * 2]; for (int i = 0; i < size; i++) { newhashArray[i] = hashArray[i]; } hashArray = newhashArray; } } public Expression lookUp(final Position key) { Node node = new Node(key, null); int hashNumber = node.getHashCode() % hashArray.length; SinglyLinkedListMap foundBucket = hashArray[hashNumber]; return foundBucket.lookUp(key); } }
- 01-31-2013, 06:24 PM #2
Similar Threads
-
UML feedback
By behedwin in forum New To JavaReplies: 0Last Post: 10-05-2012, 11:21 AM -
Feedback
By GrumpyBum in forum Suggestions & FeedbackReplies: 0Last Post: 04-25-2012, 11:34 AM -
Calculator Feedback
By 0026sd in forum New To JavaReplies: 0Last Post: 09-20-2011, 02:23 PM -
Need feedback for my program.
By Pojahn_M in forum New To JavaReplies: 3Last Post: 08-12-2011, 08:12 PM -
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 07:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks