码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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
  • 相关阅读:
    web前端网页制作课作业:用DIV+CSS技术设计的静态网站【四大名著】中国传统文化主题题材设计
    【计算机网络】 集线器、网桥、交换机、路由器看这一篇就懂了。实验: 路由器的作用,以及没有路由器的情况下,如何用三层交换机实现路由器的功能
    Github Copilot Chat 初体验
    zabbix监控nginx
    湖北省组织申报国家火炬特色产业基地条件、流程梳理
    SpringMVC基础:RestFul风格
    面试系列Spring:Spring的事务传播
    EasyRule源码:EasyRule框架源码分析
    视频剪辑助手:轻松实现视频随机分割并提取音频保存
    计算机毕业设计Java木材产销系统的生产管理模块(源代码+数据库+系统+lw文档)
  • 原文地址:https://blog.csdn.net/dodamce/article/details/125510671
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号