Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dataStructures;
- public class ChainedHashTable<K extends Comparable<K>, V>
- extends HashTable<K,V>
- {
- static final long serialVersionUID = 0L;
- // The array of dictionaries.
- protected Dictionary<K,V>[] table;
- public ChainedHashTable( int capacity )
- {
- int arraySize = HashTable.nextPrime((int) (1.1 * capacity));
- // Compiler gives a warning.
- table = (Dictionary<K,V>[]) new Dictionary[arraySize];
- for ( int i = 0; i < arraySize; i++ )
- table[i] = new OrderedDoubleList<K,V>();
- maxSize = capacity;
- currentSize = 0;
- }
- public ChainedHashTable( )
- {
- this(DEFAULT_CAPACITY);
- }
- // Returns the hash value of the specified key.
- protected int hash( K key )
- {
- return Math.abs( key.hashCode() ) % table.length;
- }
- // If there is an entry in the dictionary whose key is the specified key,
- // returns its value; otherwise, returns null.
- public V find( K key )
- {
- return table[ this.hash(key) ].find(key);
- }
- // If there is an entry in the dictionary whose key is the specified key,
- // replaces its value by the specified value and returns the old value;
- // otherwise, inserts the entry (key, value) and returns null.
- public V insert( K key, V value )
- {
- if ( this.isFull() )
- //TODO: Original comentado para nao dar erro de compilacao.
- //this.rehash();
- return null;
- //TODO: Left as an exercise.
- else { currentSize++;
- return table[this.hash(key)].insert(key, value);
- }
- }
- // If there is an entry in the dictionary whose key is the specified key,
- // removes it from the dictionary and returns its value;
- // otherwise, returns null.
- public V remove( K key )
- {
- //TODO: Left as an exercise.
- currentSize--;
- return table[this.hash(key)].remove(key) ;
- }
- // Returns an iterator of the entries in the dictionary.
- public Iterator<Entry<K,V>> iterator( ){
- // implementar iterador usndo table e currentsize
- //TODO: Left as an exercise.
- return new HashTableIterator<K,V>(table,currentSize);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement