Results 1 to 5 of 5
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Hashtable help/confusion

    I was wondering how to traverse through a hashtable. I am stuck on how to implement an iterator for the values of some hashtable.

    Java Code:
    public Iterator<V> values() {
    			return new Iterator<V>(){
    
    			@Override
    			public boolean hasNext() {
    				// TODO Auto-generated method stub
    				return false;
    			}
    
    			@Override
    			public V next() {
    				// TODO Auto-generated method stub
    				return null;
    			}
    
    			@Override
    			public void remove() {
    				// TODO Auto-generated method stub
    				
    			}
    			
    			
    		};
    Thank you for all tips and advice.

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,686
    Rep Power
    5

    Default Re: Hashtable help/confusion

    I must now guess because you fail to be specific: a Hashtable you yourself built?

    If it is the Hashtable built into the JDK you're reinventing the wheel.


    Assuming I'm correct, don't you think it would be a good idea to share how you are storing the information?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: Hashtable help/confusion

    I am implementing my own hashtable. I am still working on the get and put method, but I was wondering if I can get help on the iterators.
    I am still learning about it and this is very new to me. Any help and suggestions would be amazing. Thanks

    Java Code:
    public class HashTable<E, V> {
    
    	private static class Entry {
    
    		/** The Element  */
    		private Object key;
    		private Object data;
    
    		public Entry() {
    			this.key = null;
    			this.data = null;
    		}
    
    		public Entry(Object key, Object data) {
    			this.key = key;
    			this.data = data;
    		}
    
    		public Object getData() {
    			return data;
    		}
    
    		public void setData(Object data) {
    			this.data = data;
    		}
    
    		public Object getKey() {
    			return key;
    		}
    		public void setKey(Object key) {
    			this.key = key;
    		}
    
    
    	}
    
    	private final int defaultSize = 64;
    	private Object table[] = new Object[ defaultSize ];
    	private int numElements = 0;
    
    	
    	public HashTable() { 
    		table = new Entry[defaultSize];
    
    	}
    
    	public void put( E key, V value ) {
    
    
    		
    	}
    
    	
    
    	public V get( E key ) {
    		
    	
    }
    
    
    
    	public Iterator<E> keys() {
    		
    	}
    	
    
    
    	public Iterator<V> values() {
    		
    
    	}
    	
    
    	
    	/** Finds either the target key or the first empty slot in the
    	 *  search chain using linear probing
    	 *  @param key */
    	private int find(E key){
    
    		//Calculate the starting index
    		int index = key.hashCode() % buckets.length;
    		if(index < 0)
    			index += buckets.length;
    
    		//Increment index until an empty slot is reached
    		//or the key is found
    		while((buckets[index] != null) && (!key.equals(buckets[index]))){
    			index ++;
    
    			if(index >= buckets.length)
    				index = 0; //Wrap around.
    		}
    		return index;
    	}
    
    }

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,686
    Rep Power
    5

    Default Re: Hashtable help/confusion

    Yes well you created a more generic question that overshadows this one:

    Hashes (new to the concept)

    So now this thread is not needed anymore.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,383
    Rep Power
    5

    Default Re: Hashtable help/confusion

    Hash tables do not look for empty slots. That would be grossly inefficient. Especially when trying to retrieve a value.

    They simply continue to add the entry to whatever "bucket" is returned by the hashing algorithm and table size. The key is having a good hash code to try and uniformly disperse the items. But even a single hash code for everything will work. It just won't be efficient.

    So, if the bucket is empty, start your list there. If the bucket is not empty, then add your data to the bucket.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Hashtable question. Getting info from a hashtable
    By sindrem in forum New To Java
    Replies: 4
    Last Post: 01-19-2012, 05:00 PM
  2. hashtable
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-06-2009, 08:20 AM
  3. hashtable
    By vijayabaskar in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 08:05 AM
  4. Hashtable
    By angelicsign in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 04:30 PM
  5. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:43 AM

Tags for this Thread

Posting Permissions

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