1. 哈希表简介:
![这里写图片描述](https://img-blog.csdn.net/20171127151757780?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjE0MzQ5NTk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2. 哈希化图解
![这里写图片描述](https://img-blog.csdn.net/20171127151933852?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjE0MzQ5NTk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
3. 开发地址法
![这里写图片描述](https://img-blog.csdn.net/20171127152022774?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjE0MzQ5NTk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
4. 代码实现
/**
* @author liupenghao 员工信息类
*/
public class Info {
private String key;
private String name;
public Info(String key, String name) {
this.key = key;
this.name = name;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class HashTable {
private Info[] arr;
public HashTable() {
arr = new Info[100];
}
/**
* z指定数组初始化大小
*/
public HashTable(int maxSize) {
arr = new Info[maxSize];
}
public void insert(Info info) {
String key = info.getKey();
int hashVal = hashCode(key);
while(arr[hashVal] !=null && arr[hashVal].getName()!=null){
++hashVal;
hashVal %= arr.length;
}
arr[hashVal] = info;
}
/**
* 查找数据
*/
public Info find(String key) {
int hashVal = hashCode(key);
while(arr[hashVal] != null ){
if(arr[hashVal].getKey().equals(key)){
return arr[hashVal];
}
++hashVal;
hashVal %= arr.length;
}
return null;
}
public Info delete(String key){
int hashVal = hashCode(key);
while(arr[hashVal] != null){
if(arr[hashVal].getKey().equals(key)){
Info temp = arr[hashVal];
temp.setName(null);
return temp;
}
++hashVal;
hashVal %= arr.length;
}
return null;
}
public int hashCode(String key) {
BigInteger hashVal = new BigInteger("0");
BigInteger pow27 = new BigInteger("1");
for (int i = key.length() - 1; i >= 0; i--) {
int letter = key.charAt(i) - 96;
BigInteger letterB = new BigInteger(String.valueOf(letter));
hashVal = hashVal.add(letterB.multiply(pow27));
pow27 = pow27.multiply(new BigInteger(String.valueOf(27)));
}
return hashVal.mod(new BigInteger(String.valueOf(arr.length))).intValue();
}
public static void main(String[] args) {
String abc = "123";
HashTable ht = new HashTable();
ht.insert(new Info("a", "张三"));
ht.insert(new Info("ct", "李四"));
System.out.println(ht.find("a").getName());
System.out.println(ht.find("ct").getName());
}
}
5. 链地址法
![这里写图片描述](https://img-blog.csdn.net/20171127152247301?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjE0MzQ5NTk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
6. 实现代码
LinkList ,Info 参见上文中的,或者直接查看源码
public class HashTable {
private LinkList[] arr;
public HashTable() {
arr = new LinkList[100];
}
/**
* z指定数组初始化大小
*/
public HashTable(int maxSize) {
arr = new LinkList[maxSize];
}
public void insert(Info info) {
String key = info.getKey();
int hashVal = hashCode(key);
if (arr[hashVal] == null) {
arr[hashVal] = new LinkList();
}
arr[hashVal].addFirstNode(info);
}
/**
* 查找数据
*/
public Info find(String key) {
int hashVal = hashCode(key);
return arr[hashVal].findByData(key);
}
public Info delete(String key) {
int hashVal = hashCode(key);
return arr[hashVal].deleteDyData(key);
}
public int hashCode(String key) {
BigInteger hashVal = new BigInteger("0");
BigInteger pow27 = new BigInteger("1");
for (int i = key.length() - 1; i >= 0; i--) {
int letter = key.charAt(i) - 96;
BigInteger letterB = new BigInteger(String.valueOf(letter));
hashVal = hashVal.add(letterB.multiply(pow27));
pow27 = pow27.multiply(new BigInteger(String.valueOf(27)));
}
return hashVal.mod(new BigInteger(String.valueOf(arr.length))).intValue();
}
public static void main(String[] args) {
String abc = "123";
HashTable ht = new HashTable();
ht.insert(new Info("a", "张三"));
ht.insert(new Info("ct", "李四"));
System.out.println(ht.find("a").getName());
System.out.println(ht.find("ct").getName());
}
}