Results 1 to 2 of 2
  1. #1
    lo2
    lo2 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default 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?
    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); 
    	  }
    	 }
    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?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,238
    Rep Power
    19

    Default Re: Feedback on this implementation of a HashMap

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. UML feedback
    By behedwin in forum New To Java
    Replies: 0
    Last Post: 10-05-2012, 11:21 AM
  2. Feedback
    By GrumpyBum in forum Suggestions & Feedback
    Replies: 0
    Last Post: 04-25-2012, 11:34 AM
  3. Calculator Feedback
    By 0026sd in forum New To Java
    Replies: 0
    Last Post: 09-20-2011, 02:23 PM
  4. Need feedback for my program.
    By Pojahn_M in forum New To Java
    Replies: 3
    Last Post: 08-12-2011, 08:12 PM
  5. Replies: 7
    Last Post: 12-08-2009, 07:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •