如何快速手写一个属于自己的hashmap(简化版)

不多说 直接上代码

package chn.xx.com.hashmap;

public class ChnHashMap {

    //简单实现,故不实现红黑树,resize扩容方法。
    //固定数组
    private Node[] nodes = new Node[10000];

    //获取数组索引方法
    private int getindex(int key){
        //计算key的hash值
        int hash = Integer.hashCode(key);
        //干扰函数,与自身高位或与,尽量减少碰撞
        hash ^= hash >> 16;

        //确定索引位置,高位清0,低位确定索引位置
        //jdk8使用(数组容量-1 与 hash值进行位与操作计算出数组索引)
        return  9999 & hash;
    }

    //put方法
    public void put(int key,int value){
        //创建新节点
        Node newn = new Node(key, value);

        //确定新节点数组下标
        int index = getindex(key);

        //获取下标对应数组的首节点
        Node node = nodes[index];

        //如果首节点为空则直接创建新的首节点
        if ( null != node){

            //记录末节点(末节点的next为null)
            Node prev = null;

            //遍历链表
            while(null != node){

                //若存在key值一样的节点,则覆盖原节点的value
                if (node.key == key){
                    node.value = value;
                    return;
                }

                prev = node;
                //指向下一个节点
                node = node.next;
            }

            //设置末节点的下一个节点为新节点
            prev.next = newn;

        } else{
            //如果为空,则首节点为新节点
            nodes[index] = newn;
        }
    }

    //get方法
    public Integer get(int key){

        //确定该key对应的数组位置
        int index = getindex(key);

        //获取对应的首节点
        Node node = nodes[index];

        //遍历链表,获取key相同的节点的value
        while (node != null){
            if (node.key == key){

                return node.value;

            }
            node = node.next;
        }

        //没找到返回null
        return null;
    }

    public void remove(int key){

        //确定该key对应的数组位置
        int index = getindex(key);

        //获取对应的首节点
        Node node = nodes[index];

        //记录当前节点
        Node re = null;

        while (node != null){

            if (node.key == key){
                //删除时判断
                //如果是首节点,则直接将数组下标对应的值指向下一个节点
                //如果不首节点,则将上一个节点的next指向下一个节点
                if (re != null){
                    re.next = node.next;
                }else{
                    nodes[index] = node.next;
                }
            }

            re = node;
            node = node.next;
        }

    }

}

测试

package chn.xx.com.test;

import chn.xx.com.hashmap.ChnHashMap;

public class HashTest {

    public static void main(String[] args) {

        ChnHashMap chn = new ChnHashMap();

        chn.put(1,1);
        System.out.println(chn.get(1));

        chn.put(1,2);
        System.out.println(chn.get(1));

        chn.put(2,2);
        System.out.println(chn.get(2));

        System.out.println(chn.get(3));

        chn.remove(1);
        System.out.println(chn.get(1));
    }

}

测试结果

1
2
2
null
null
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值