哈希冲突解决---开散列/哈希表

```java
```java
public class HashBuck {
    static class Node {
        public int key;
        public int val;
        public Node next;
        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    public Node[] array;
    public int usedSize;

    public HashBuck() {
        this.array = new Node[8];
    }


    public void push(int key,int val) {
        Node node = new Node(key,val);
        int index = key % array.length;
        Node cur = array[index];
        while (cur != null) {
            if(cur.key == key) {
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        //开始进行头插法  实际上JDK8  是尾插法
        node.next = array[index];
        array[index] = node;
        this.usedSize++;
        if(loadFactor() >= 0.75) {
            resize();
        }
    }

    /**
     * 求负载因子
     * @return
     */
    public double loadFactor() {
        return this.usedSize*1.0 / this.array.length;
    }

    /**
     * 超过了负载因子 我们需要进行扩容
     * 作业:去看源码当中的,如果 超过了负载因子,扩容是怎么样扩容?
     */
    public void resize() {
        Node[] newArray = new Node[2*array.length];
        //遍历原来的数组,把原来数组里面的每个元素都进行重新哈希
        for (int i = 0; i < array.length; i++) {
            //cur是每个数组元素,下标对象的链表的头
            Node cur = array[i];
            while (cur != null) {
                int index = cur.key % newArray.length;//确定的是新数组的位置
                Node curNext = cur.next;//保存一份
                cur.next = newArray[index];//开始头插
                newArray[index] = cur;
                cur = curNext;
            }
        }
        //原来数组当中的数据 全部哈希到了 新的数组当中
        array = newArray;
    }

    public int get(int key) {
        int index = key % array.length;
        Node cur = array[index];
        while (cur != null) {
            if(cur.key == key) {
                return cur.val;
            }
            cur = cur.next;
        }
        return -1;//没找到
    }

    public static void main(String[] args) {
        HashBuck hashBuck = new HashBuck();
        hashBuck.push(1,1);
        hashBuck.push(2,2);
        hashBuck.push(10,10);
        hashBuck.push(4,4);
        hashBuck.push(5,5);
        hashBuck.push(6,6);//这个元素放完后就要扩容了
        hashBuck.push(7,7);
        System.out.println(hashBuck.get(6));

        HashMap<String,Integer> map = new HashMap<>();

    }
}



class Person {
    public Integer id;

    public Person(Integer id) {
        this.id = id;
    }
//需要重写哈希code和equals方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(id, person.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                '}';
    }
}
public class HashBuck2<K,V> {

    static class Node<K,V> {
        public K key;
        public V val;
        public Node<K,V> next;

        public Node(K key, V val) {
            this.key = key;
            this.val = val;
        }
    }

    public Node<K,V>[] array;
    public int usedSize;

    public HashBuck2() {
        this.array = new Node[8];
    }

    public void push(K key,V val) {
        Node<K,V> node = new Node<K,V>(key,val);
        int hash = key.hashCode();
        int index = hash % array.length;
        Node<K,V> cur = array[index];
        while (cur != null) {
            if(cur.key.equals(key)) {
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        //开始进行头插法  实际上JDK8  是尾插法
        node.next = array[index];
        array[index] = node;
        this.usedSize++;
    }

    public V get(K key) {
        int hash = key.hashCode();
        int index = hash % array.length;
        Node<K,V> cur = array[index];
        while (cur != null) {
            if(cur.key == key) {
                return cur.val;
            }
            cur = cur.next;
        }
        return null;//没找到
    }

    public static void main(String[] args) {
        Person person1 = new Person(1);
        Person person2 = new Person(2);
        HashBuck2<Person,String> hashBuck2 = new HashBuck2<>();
        hashBuck2.push(person1,"gaobo");
        hashBuck2.push(person2,"wangfei");
        System.out.println(hashBuck2.get(person1));
       /* Person person1 = new Person(1);
        Person person2 = new Person(2);
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());*/
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值