1:哈希表的概念
2:设计原理
3:哈希表的Java设计
package hashTable;
import java.util.HashMap;
/**
* @author :dazhu
* @date :Created in 2020/3/20 15:21
* @description:哈希表的学习
* @modified By:
* @version: $
*/
public class Main {
public static void main(String[] args){
HashTable ht = new HashTable(10,1);
ht.insert(0);
ht.insert(1);
ht.insert(2);
ht.insert(3);
ht.insert(4);
ht.insert(5);
ht.insert(6);
ht.insert(7);
ht.insert(8);
ht.insert(9);
ht.insert(13);
ht.insert(14);
ht.insert(15);
ht.insert(23);
ht.insert(24);
ht.insert(25);
ht.insert(33);
ht.insert(34);
ht.insert(35);
}
}
class HashTable{
public Node[] array;//内置数组
public int factor ;//装载因子
public HashTable(int length,int factor){
this.array = new Node[length];
this.factor = factor;
}
public void insert(int a){
//如果是第一次插入,则放入array中,
//如果已经数据再该位置,则加载该位置后面的链表中。链表的head。
if(array[hashCode(a)]==null){
array[hashCode(a)] = new Node(a);
}
else{
insertList(a);
}
}
public void delete(int a){
}
/**
* 始终使新加入数据节点,存放在链表的head
* @param a 待加入数据
*/
public void insertList(int a){
//当前数据的hashCode数组中存放的元素
Node headNode = array[hashCode(a)];
//当前数据节点
Node temp = new Node(a);
//新数据节点指向原来的head。
temp.next = headNode;
//然后将head存入数组中
array[hashCode(a)] = temp;
}
/**
*
* @param a 待输入数
* @return 待输入数的hashcode。
* 使用取模的方法来获取当前数的hashcode
*/
public int hashCode(int a){
return a%array.length;
}
/**
* 解决hash冲突的链表节点
*/
class Node{
public int val;
public Node next;
public Node(int val){
this.val = val;
}
}
}