Results 1 to 5 of 5
Thread: Hashtable help/confusion
- 03-12-2014, 02:35 PM #1
Senior Member
- Join Date
- Mar 2013
- Posts
- 123
- Rep Power
- 0
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 } };
- 03-12-2014, 02:41 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 03-12-2014, 03:07 PM #3
Senior Member
- Join Date
- Mar 2013
- Posts
- 123
- Rep Power
- 0
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; } }
- 03-12-2014, 03:37 PM #4
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Hashtable help/confusion
Yes well you created a more generic question that overshadows this one:
http://www.java-forums.org/new-java/...w-concept.html
So now this thread is not needed anymore."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 03-12-2014, 03:45 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Hashtable question. Getting info from a hashtable
By sindrem in forum New To JavaReplies: 4Last Post: 01-19-2012, 06:00 PM -
hashtable
By vijayabaskar in forum Java ServletReplies: 0Last Post: 04-06-2009, 09:20 AM -
hashtable
By vijayabaskar in forum Advanced JavaReplies: 2Last Post: 04-06-2009, 09:05 AM -
Hashtable
By angelicsign in forum New To JavaReplies: 6Last Post: 02-05-2009, 05:30 PM -
Hashtable example
By Java Tip in forum Java TipReplies: 0Last Post: 02-15-2008, 09:43 AM
Bookmarks