1、什么是hash表:给定表M,存在f(key),对任意给定的关键值key,带入函数后,若能得到包含 该关键字的记录在表中的地址。则称表M为hash表。f(key)称为hash函数
2、给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
这道题的思路就是上面的图。
class Solution {
public int firstUniqChar(String s) {
int [] arr = new int[26];
for(int i = 0;i<s.length();i++) {
arr[s.charAt(i)-'a']++;
}
for(int i = 0;i<s.length();i++) {
if(arr[s.charAt(i)-'a']==1) {
return i;
}
}
return -1;
}
}
3、hash函数设计原则
4、如何设计hash函数
5、Java中对hash值的处理
Java中含有一个hashcode()函数,可以将Interger Double等类型的对象模型直接计算出hash值,
那我们如何计算出自定义的对象的hash值呢。
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
//自己重写hash函数
@Override
public int hashCode() {
int hash = 0;
//定义一个进制
int B = 666;
//计算hash
hash = hash*B + name.hashCode();
hash = hash*B + ((Integer)age).hashCode();
return hash;
}
//自己重写equals()函数
@Override
public boolean equals(Object obj) {
if(this==obj) {
return true;
}
if(obj==null || obj.getClass()!=this.getClass()) {
return false;
}
Student student = (Student)obj;
if(this.name.equals(student.name)&& this.age==student.age){
return true;
}
return false;
}
}
public class Demo08 {
public static void main(String[] args) {
Student student = new Student("tom",18);
Student student2 = new Student("tom",18);
//自定义的hashcode()函数对于同一个对象算出来的hashcode()值是相同的,
//系统自带的hashcode()算出来的就不同
System.out.println(student.hashCode());
System.out.println(student2.hashCode());
HashSet<Student> set = new HashSet<Student>();
set.add(student);
set.add(student2);
//打印对象的个数 对象的比较不是通过hash值来比较的,而是通过equals()方法来比较的
System.out.println(set.size());
}
}
结果:76607334 76607334 1
6、哈希冲突的解决方案
7、Java中HashMap的原理解读
主要关注的重点是:如何通过key值去找我们的哈希表数组的索引。
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
//自己重写hash函数
@Override
public int hashCode() {
int hash = 0;
//定义一个进制
int B = 666;
//计算hash
hash = hash*B + name.hashCode();
hash = hash*B + ((Integer)age).hashCode();
return hash;
}
//自己重写equals()函数
@Override
public boolean equals(Object obj) {
if(this==obj) {
return true;
}
if(obj==null || obj.getClass()!=this.getClass()) {
return false;
}
Student student = (Student)obj;
if(this.name.equals(student.name)&& this.age==student.age){
return true;
}
return false;
}
}
import java.util.TreeMap;
//自定义一个哈希表
public class HashTable<K,V> {
//定义一个treeMap类型的数组
private TreeMap<K, V> [] hashtable;
//数组中存放元素的个数
private int size;
//数组的容量
private int M;
//定义构造方法
public HashTable(int M) {
this.M = M;
size = 0;
hashtable = new TreeMap[M];
//将数组中的每一个元素都初始化一下
for(int i = 0;i<M;i++) {
hashtable[i] = new TreeMap<>();
}
}
//定义一个无参构造方法,用来定义数组的初始长度
public HashTable() {
this(16);
}
//1、获取hash表中的数据个数
public int getSize() {
return size;
}
//2、通过hash(key)中的key去获得hash表中对应的下表
public int hash(K key) {
return (key.hashCode()&0x7777777)%M;
}
//3、添加操作
/**
* 如果key值存在
* 如果key值不存在
* @param key
* @param value
*/
public void add(K key,V value) {
//1.根据key值,找到对应的索引位置
int index = hash(key);
//2.根据索引下表去获取每个哈希桶里面的Tree
TreeMap<K,V> treeMap = hashtable[index];
if(treeMap.containsKey(key)) {
//执行修改操作
treeMap.put(key, value);
}else {
//执行添加操作
treeMap.put(key, value);
//修改size值
size++;
}
}
//4、根据key值删除元素
public V remove(K key) {
//根据key值找到对应的索引
int index = hash(key);
//根据索引找到里面的树
TreeMap<K,V> treeMap = hashtable[index];
if(treeMap.containsKey(key)) {
//执行删除操作
V value = treeMap.remove(key);
return value;
//维护size值
size--;
}
return null;
}
//5.通过key值,去获取他的value值
public V get(K key) {
return hashtable[hash(key)].get(key);
}
//6.判断key是否存在
public boolean contains(K key) {
return hashtable[hash(key)].containsKey(key);
}
}
8、Java中HashMap底层put的过程
通过key算出这个节点的哈希值,然后将哈希值取模,放到对应索引数组的位置。
9、HashMap—hash(key)方法详解
10、总结:哈希函数的底层就是一个数组+链表的形式,他的核心知识点就是通过 key值找到数组 对应的下标。哈希表存值的方式就是key—value的形式,看作是一个节点。