• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

java分离链式法实现 HashTable

JAVA相关 水墨上仙 1376次浏览

java分离链式法实现 HashTable

JDK中的相应Hashtable中并没有使用LinkedList,而是使用自封装的Entry,显得使其更紧凑,且没有冗余。

protected Entry(int hash, K key, V value, Entry<K,V> next) {
	    this.hash = hash;
	    this.key = key;
	    this.value = value;
	    this.next = next;
	}

而下面程序使用了LinkedList,使代码更简单更清晰易懂。

package changsheng.algorithms.hash;
import java.util.LinkedList;
public class SeparateChainingHashTable<T> {
	/**
	 * Construct the hash table.
	 */
	public SeparateChainingHashTable() {
		this(DEFAULT_TABLE_SIZE);
	}
	/**
	 * Construct the hash table.
	 * 
	 * @param size
	 *            approximate table size.
	 */
	@SuppressWarnings("unchecked")
	public SeparateChainingHashTable(int size) {
		theLists = (LinkedList<T>[]) new LinkedList[nextPrime(size)];
		for (int i = 0; i < theLists.length; i++)
			theLists[i] = new LinkedList<T>();
	}
	/**
	 * Insert into the hash table. If the item is already present, then do
	 * nothing.
	 * 
	 * @param x
	 *            the item to insert.
	 */
	public void insert(T x) {
		LinkedList<T> whichList = theLists[x.hashCode() % theLists.length];
		if (!whichList.contains(x)) {
			whichList.addFirst(x);
		}
	}
	/**
	 * Remove from the hash table.
	 * 
	 * @param x
	 *            the item to remove.
	 */
	public boolean remove(T x) {
		return theLists[x.hashCode() % theLists.length].remove(x);
	}
	/**
	 * Find an item in the hash table.
	 * 
	 * @param x
	 *            the item to search for.
	 * @return the matching item, or null if not found.
	 */
	public boolean find(T x) {
		return theLists[x.hashCode() % theLists.length].contains(x);
	}
	/**
	 * Make the hash table logically empty.
	 */
	public void makeEmpty() {
		for (int i = 0; i < theLists.length; i++)
			theLists[i].clear();
	}
	private static final int DEFAULT_TABLE_SIZE = 101;
	/** The array of Lists. */
	private LinkedList<T>[] theLists;
	/**
	 * Internal method to find a prime number at least as large as n.
	 * 
	 * @param n
	 *            the starting number (must be positive).
	 * @return a prime number larger than or equal to n.
	 */
	private static int nextPrime(int n) {
		if (n % 2 == 0)
			n++;
		for (; !isPrime(n); n += 2)
			;
		return n;
	}
	/**
	 * Internal method to test if a number is prime. Not an efficient algorithm.
	 * 
	 * @param n
	 *            the number to test.
	 * @return the result of the test.
	 */
	private static boolean isPrime(int n) {
		if (n == 2 || n == 3)
			return true;
		if (n == 1 || n % 2 == 0)
			return false;
		for (int i = 3; i * i <= n; i += 2)
			if (n % i == 0)
				return false;
		return true;
	}
	// Simple main
	public static void main(String[] args) {
		SeparateChainingHashTable<Integer> H = new SeparateChainingHashTable<Integer>();
		final int NUMS = 4000;
		final int GAP = 37;
		System.out.println("Checking... (no more output means success)");
		for (int i = GAP; i != 0; i = (i + GAP) % NUMS)
			H.insert(new Integer(i));
		for (int i = 1; i < NUMS; i += 2)
			H.remove(new Integer(i));
		for (int i = 2; i < NUMS; i += 2)
			if (!H.find(new Integer(i)))
				System.out.println("Find fails " + i);
		for (int i = 1; i < NUMS; i += 2) {
			if (H.find(new Integer(i)))
				System.out.println("OOPS!!! " + i);
		}
	}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明java分离链式法实现 HashTable
喜欢 (0)
加载中……