Download code

Jump to: navigation, search

Back to Hash_table_(Java)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

Hashtbl.java

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Hash_table_(Java)?oldid=12352
 14 */
 15 
 16 class Hashnode<K,V> {
 17     K key;
 18     V data;
 19     Hashnode<K,V> next;
 20 
 21     public Hashnode(K k, V v, Hashnode<K,V> n)
 22     { key = k; data = v; next = n; }
 23 }
 24 
 25 public class Hashtbl<K,V> {
 26     private Hashnode<K,V>[] nodes;
 27 
 28     @SuppressWarnings("unchecked")
 29     public Hashtbl(int size)
 30     {
 31         nodes = new Hashnode[size];
 32     }
 33 
 34     private int getIndex(K key)
 35     {
 36         int hash = key.hashCode() % nodes.length;
 37         if (hash < 0)
 38             hash += nodes.length;
 39         return hash;
 40     }
 41 
 42     public V insert(K key, V data)
 43     {
 44         int hash = getIndex(key);
 45 
 46     //    System.err.printf("insert() key=%s, hash=%d, data=%s\n", key, hash, data);
 47 
 48         for (Hashnode<K,V> node = nodes[hash]; node != null; node = node.next) {
 49             if (key.equals(node.key)) {
 50                 V oldData = node.data;
 51                 node.data = data;
 52                 return oldData;
 53             }
 54         }
 55 
 56         Hashnode<K,V> node = new Hashnode<K,V>(key, data, nodes[hash]);
 57         nodes[hash] = node;
 58 
 59         return null;
 60     }
 61 
 62     public boolean remove(K key)
 63     {
 64         int hash = getIndex(key);
 65 
 66         Hashnode<K,V> prevnode = null;
 67         for (Hashnode<K,V> node = nodes[hash]; node != null; node = node.next) {
 68             if (key.equals(node.key)) {
 69                 if (prevnode != null)
 70                     prevnode.next = node.next;
 71                 else
 72                     nodes[hash] = node.next;
 73                 return true;
 74             }
 75             prevnode = node;
 76         }
 77 
 78         return false;
 79     }
 80 
 81     public V get(K key)
 82     {
 83         int hash = getIndex(key);
 84 
 85     //    System.err.printf("get() key=%s, hash=%d\n", key, hash);
 86 
 87         for (Hashnode<K,V> node = nodes[hash]; node != null; node = node.next) {
 88             if (key.equals(node.key))
 89                 return node.data;
 90         }
 91 
 92         return null;
 93     }
 94     public void resize(int size)
 95     {
 96         Hashtbl<K,V> newtbl = new Hashtbl<K,V>(size);
 97 
 98         for (Hashnode<K,V> node : nodes) {
 99             for ( ; node != null; node = node.next) {
100                 newtbl.insert(node.key, node.data);
101                 remove(node.key);
102             }
103         }
104 
105         nodes = newtbl.nodes;
106     }
107 
108     public static void main(String[] args)
109     {
110         Hashtbl<String,String> hashtbl = new Hashtbl<String,String>(16);
111 
112         hashtbl.insert("France", "Paris");
113         hashtbl.insert("England", "London");
114         hashtbl.insert("Sweden", "Stockholm");
115         hashtbl.insert("Germany", "Berlin");
116         hashtbl.insert("Norway", "Oslo");
117         hashtbl.insert("Italy", "Rome");
118         hashtbl.insert("Spain", "Madrid");
119         hashtbl.insert("Estonia", "Tallinn");
120         hashtbl.insert("Netherlands", "Amsterdam");
121         hashtbl.insert("Ireland", "Dublin");
122 
123         System.out.println("After insert:");
124         String italy = hashtbl.get("Italy");
125         System.out.println("Italy: " + (italy != null ? italy : "-"));
126         String spain = hashtbl.get("Spain");
127         System.out.println("Spain: " + (spain != null ? spain : "-"));
128 
129         hashtbl.remove("Spain");
130 
131         System.out.println("After remove:");
132         spain = hashtbl.get("Spain");
133         System.out.println("Spain: " + (spain != null ? spain : "-"));
134 
135         hashtbl.resize(8);
136 
137         System.out.println("After resize:");
138         italy = hashtbl.get("Italy");
139         System.out.println("Italy: " + (italy != null ? italy : "-"));
140     }
141 }
142 


hijacker
hijacker
hijacker
hijacker