惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - 小猫钓鱼吃鱼

把Claude Code玩明白:VS Code零成本接入DeepSeek大模型 硅基流动-免费代金券 告别从零造轮子!我的 Electron+Vue3 脚手架,让你 10 秒启动桌面应用开发 Electron劝退指南?不,这是我的‘真香’致富经 一个面向产品化的 Electron + Vue 3 桌面应用脚手架 动态代理 自己实现Linkedlist,实现其常用的增、删、查的方法 自己实现Arraylsit,实现其常用的几种增、删、该、查的方法 前后端分离 微服务项目 通用后台管理系统 easypoi一行代码搞定excel导入导出 如何跳出页面的Frame框架 如何从GitHub上下载部分自己需要的文件 Mysql Explain 详解 解决 spring boot Failed to decode downloaded font Java中List集合的三种遍历方式(全网最详) SpringBoot设置首页(默认页)跳转功能的实现方案 idea中搭建ssm框架,详细步骤 微信公众号分享知识 idea创建maven项目速度慢?别急,这有三种方案 ceph是什么 redis 注册开机自启动服务(注意:要到你安装redis的根目录下执行下面的cmd命令) 怎么让mac和win/pc互传文件共享文件传输文件 Starting MySQL... ERROR! The server quit without updating PID file 问题解决
自己实现HashMap
小猫钓鱼吃鱼 · 2021-06-09 · via 博客园 - 小猫钓鱼吃鱼
/**
 * hashmap的demo
 * @author cuiyc
 * @version 1.0
 * @date 2021/6/5 22:28
 */
public class HashMapDemo {
    public static void main(String[] args) {

        Map<String,String> hashmap=new HashMap<>();

        MyHashMap map=new MyHashMap();
        map.put("k1","v1");
        map.put("k2","v2");
        System.out.println("map的长度是,"+map.size);
        System.out.println("k2的value是,"+map.get("k2"));
        System.out.println("k1的value是,"+map.get("k1"));
    }

    private static class MyHashMap{
        private int size;
        LinkedList[] array =new LinkedList[999];


        public MyHashMap(){}
        public MyHashMap(int size){
            this.size=size;
        }

        //计算key的hashcode值
        final int hash(Object key){
            int h;
            return (key==null)?0:(h=key.hashCode())^(h>>>16);
        }
        //对数组的长度取模,得到应该存入的位置
        final int mode(int h){
            return h/array.length;
        }

        //得到size的值
        public int getSize(){
            return size;
        }
        //put方法
        public void put(Object key,Object value){
            //先把key 和 value放入entry对象中
            MyEntry myEntry=new MyEntry(key,value);
            //根据key值计算出hashcode值,然后得到应当在数组中存放的位置
            int hashValue=hash(key);
            //然后拿到hahscode值后,再和数组的长度取模计算出再数组中的一个位置
            hashValue=mode(hashValue);
            //若该位置为空,则直接添加链表
            if(array[hashValue]==null){
                LinkedList linkedList=new LinkedList();
                linkedList.add(myEntry);
                array[hashValue]=linkedList;
            }else {
                //否则,该位置是已经相同的key值,则需要遍历到key值的entry,然后更新。
                for (int i = 0; i < array[hashValue].size(); i++) {
                     MyEntry myentry= (MyEntry) array[hashValue].get(i);
                     if(key.equals(myentry.getKey())){
                         myentry.value=value;
                     }else {
                         array[hashValue].add(myEntry);
                     }
                }
            }
            size++;
        }

        //get方法
        public Object get(Object Key){
            int hashCode=mode(Key.hashCode());
            if(array[hashCode]!=null){
                for (int i = 0; i < array[hashCode].size(); i++) {
                   MyEntry myEntry= (MyEntry) array[hashCode].get(i);
                   if(Key.equals(myEntry.getKey())){
                       return myEntry.value;
                   }
                }
            }
            return null;
        }
    }

    //Entry数据结构
    private static class MyEntry<K,V> implements Map.Entry<K,V>{
        int hash;
        final K key;
        V value;
        MyEntry<K,V> next;

        MyEntry(int hash,K key,V value,MyEntry<K,V> next){
            this.hash=hash;
            this.key=key;this.value=value;this.next=next;
        }

        public MyEntry(K key,V value){
            this.key=key;this.value=value;
        }



        @Override
        public K getKey() {
            return key;
        }

        @Override
        public V getValue() {
            return value;
        }

        @Override
        public V setValue(V newValue) {
            V oldValue=value;
            value=newValue;
            return oldValue;
        }

        @Override
        public final boolean equals(Object o) {
            if (o == this){
                return true;
            }
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                if (Objects.equals(key, e.getKey()) &&
                        Objects.equals(value, e.getValue())){
                    return true;
                }
            }
            return false;
        }

        @Override
        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }


    }
}