• JavaSE_Java实现开散列哈希桶,测试重载hashCode方法


    C++哈希函数_哈希表_哈希冲突_负载因子_仿函数_针对字符串的模板特化特化_闭散列(线性探测)哈希表_开散列哈希桶的模拟实现(Key_Value模型数组

    具体思路看上面的链接,这里直接给出Java代码

    import java.util.HashMap;
    import java.util.Objects;
    
    class Hash {
        static class Node {
            public int key;
            public int val;
            public Node next;
    
            public Node(int key, int val) {
                this.key = key;
                this.val = val;
            }
        }
    
        private Node[] array;
        public int useSize;
    
        public Hash() {
            this.useSize = 0;
            this.array = new Node[10];
        }
    
        public void put(int key, int val) {
            //找到key所在的位置
            int index = key % this.array.length;
            //遍历链表,看是否有相同的key
            Node node = this.array[index];
            while (node != null) {
                if (node.key == key) {
                    node.val = val;
                    return;
                }
                node = node.next;
            }
            //没有这个元素,头插法
            //先检查负载因子
            this.useSize++;
            if (loadFacter() > DEFAULT_LOADFACTER) {
                //扩容
                resize();
            }
            node = new Node(key, val);
            node.next = this.array[index];
            this.array[index] = node;
        }
    
        public int get(int key) {
            int index = key % this.array.length;
            Node node = this.array[index];
            while (node != null) {
                if (node.key == key) {
                    return node.val;
                }
                node = node.next;
            }
            return -1;//没找到返回-1
        }
    
        public static final double DEFAULT_LOADFACTER = 0.75;
    
        private double loadFacter() {
            return 1.0 * this.useSize / this.array.length;
        }
    
        private void resize() {
            Node[] new_array = new Node[2 * this.array.length];
            for (int i = 0; i < this.array.length; i++) {
                Node node = this.array[i];
                while (node != null) {
                    int index = node.key % new_array.length;
                    //记录下一个节点
                    Node next = node.next;
                    node.next = new_array[index];
                    new_array[index] = node;
                    node = next;
                }
            }
            this.array = new_array;
        }
    }
    
    //测试HashCode
    class Person {//自定义类型重写hashCode函数
        public String name;
    
        public Person(String name) {
            this.name = name;
        }
    
        @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(name, person.name);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(name);
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
    
    public class HashCode {
        public static void main(String[] args) {
            Hash hash = new Hash();
            for (int i = 0; i < 20; i++) {
                hash.put(i, i + 1);
            }
            for (int i = 20; i >= 0; i--) {
                System.out.print(hash.get(i) + " ");
            }
            System.out.println();
            System.out.println(new Person("123").hashCode());
            System.out.println(new Person("123").hashCode());
            HashMap<Person, Integer> hashMap = new HashMap<>();
            Person person = new Person("Hello");
            hashMap.put(person, 1);//需要重写hashCode方法
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
  • 相关阅读:
    Java开源工作流引擎有什么突出特点?
    XUbuntu22.04之解决桌面突然放大,屏幕跟着鼠标移动问题(一百九十)
    记录 Ucharts 的使用
    【Java 实战】通过Redis实现购物车功能
    RN封装的组件库
    WHAT - React 学习系列(一)
    如何飞速成为开源贡献者(Contributor)
    Go 小知识之 Go 中如何使用 set
    java教师科研成果管理系统
    电影票小程序插件 电影票CPS插件 电影票微信小程序插件
  • 原文地址:https://blog.csdn.net/dodamce/article/details/125510671