Hash table with separate chaining : HashTable Map « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » HashTable Map 




Hash table with separate chaining
     
import java.io.IOException;

class Link {
  private int data;
  public Link next;

  public Link(int d) {
    data = d;
  }

  public int getKey() {
    return data;
  }

  public void displayLink() {
    System.out.print(data + " ");
  }
}

class SortedList {
  private Link first;
  public SortedList() {
    first = null;
  }

  public void insert(Link theLink){
    int key = theLink.getKey();
    Link previous = null// start at first
    Link current = first;
    // until end of list,
        //or current bigger than key,
    while (current != null && key > current.getKey()) { 
      previous = current;
      current = current.next; // go to next item
    }
    if (previous == null// if beginning of list,
      first = theLink; 
    else
      // not at beginning,
      previous.next = theLink; 
    theLink.next = current; 
  }

  public void delete(int key){ 
    Link previous = null
    Link current = first;

    while (current != null && key != current.getKey()) { 
      previous = current;
      current = current.next; 
    }
    // disconnect link
    if (previous == null//   if beginning of list delete first link
      first = first.next;       
    else
      //   not at beginning
      previous.next = current.next; //delete current link
  }

  public Link find(int key) {
    Link current = first; 
    while (current != null && current.getKey() <= key) { // or key too small,
      if (current.getKey() == key// found, return link
        return current;  
      current = current.next; // go to next item
    }
    return null// cannot find it
  }

  public void displayList() {
    System.out.print("List: ");
    Link current = first;
    while (current != null){
      current.displayLink()
      current = current.next;
    }
    System.out.println("");
  }
}

public class HashChain {
  private SortedList[] hashArray; 

  private int arraySize;

  public HashChain(int size) {
    arraySize = size;
    hashArray = new SortedList[arraySize];
    for (int i = 0; i < arraySize; i++)
      hashArray[inew SortedList()
  }

  public void displayTable() {
    for (int j = 0; j < arraySize; j++) {
      System.out.print(j + ". ")
      hashArray[j].displayList()
    }
  }

  public int hashFunc(int key) {
    return key % arraySize;
  }

  public void insert(Link theLink) {
    int key = theLink.getKey();
    int hashVal = hashFunc(key)
    hashArray[hashVal].insert(theLink)
  }

  public void delete(int key) {
    int hashVal = hashFunc(key)// hash the key
    hashArray[hashVal].delete(key)
  }

  public Link find(int key) {
    int hashVal = hashFunc(key)// hash the key
    Link theLink = hashArray[hashVal].find(key)// get link
    return theLink;
  }

  public static void main(String[] argsthrows IOException {
    int aKey;
    Link dataItem;
    int size, initSize, keysPerCell = 100;
    size = 100;
    initSize = 10;
    HashChain hashTable = new HashChain(size);

    for (int i = 0; i < initSize; i++){
      aKey = (int) (java.lang.Math.random() * keysPerCell * size);
      dataItem = new Link(aKey);
      hashTable.insert(dataItem);
    }
    hashTable.displayTable();
    aKey = 100;
    dataItem = new Link(aKey);
    hashTable.insert(dataItem);
    aKey = 100;
    hashTable.delete(aKey);

    aKey = 50;
    dataItem = hashTable.find(aKey);
    if (dataItem != null)
      System.out.println("Found " + aKey);
    else
      System.out.println("Could not find " + aKey);
  }

}

           
         
    
    
    
    
  














Related examples in the same category
1.Check if a particular key exists in Java Hashtable
2.Check if a particular value exists in Java Hashtable
3.Get Collection of Values from Java Hashtable
4.Get Set view of Keys from Java Hashtable
5.Get Size of Java Hashtable
6.Iterate through keys of Java Hashtable
7.Remove all values from Java Hashtable
8.Scan the content of a hashtable
9.Remove value from Java Hashtable
10.Sort keys in an Hashtable
11.Associates keys with valuesAssociates keys with values
12.Iterate through values of Java Hashtable
13.A simple Map implementationA simple Map implementation
14.Hash table with linear probingHash table with linear probing
15.Hash table with double hashingHash table with double hashing
16.Working with Key-Value Pairs in a Hashtable
17.Demonstrate the Hashtable class, and an Enumeration
18.Demonstrate the HashMap class, and an IteratorDemonstrate the HashMap class, and an Iterator
19.Soft HashMap
20.MultiMap extends AbstractMap
21.Array Map extends AbstractMapArray Map extends AbstractMap
22.Demonstrating the WeakHashMapDemonstrating the WeakHashMap
23.Use treemapUse treemap
24.Sorting Elements in a TreeMapSorting Elements in a TreeMap
25.What you can do with a TreeMapWhat you can do with a TreeMap
26.A Map implemented with ArrayLists
27.Simple demonstration of HashMapSimple demonstration of HashMap
28.HashMap
29.Caching Hashtable
30.Hashtable that supports mostly-concurrent reading, but exclusive writing.
31.Lru Hashtable
32.Bucketized Hashtable
33.Custom hash table based on customized array
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.