手撕HashMap

  • 实现一个简单的hashMap,包括get()  put()  remove(),并包含扩容和缩容。
    import java.util.Objects;
    
    public class MyHashMap<K,V>{
        public Node[] hashTable;
        static int INTCAPACITY=4;
        //    计算当前hashTable中的元素个数
        static int CURSIZE=0;
        static float FACTOR= 0.75F;
        //    静态节点成员
        static class Node<K,V>{
            K key;
            V value;
            Node next;
    
            public Node(K key,V value){
                this.key=key;
                this.value=value;
                this.next=null;
            }
        }
    
        //    初始构造函数
        public MyHashMap(){
            this.hashTable=new Node[INTCAPACITY];
            for(int i=0;i<hashTable.length;i++){
                hashTable[i]=new Node<>(-1,0);
            }
        }
        //    计算hash值(table下标)
        public int getHash(K key,int length){
            return Objects.hashCode(key)%length;
        }
    
        public V get(K key){
            int index=getHash(key,hashTable.length);
            Node head=hashTable[index];
            while(head!=null && head.key!=key){
                head=head.next;
            }
            if(head==null){
                throw new RuntimeException("Key Not Found\n");
            }
            if(head.key.equals(key)){
                return (V) head.value;
            }
            return null;
        }
        public void put(K key,V value){
            ifAddCapacity();
            int index=getHash(key,hashTable.length);
            Node head=hashTable[index];
            Node pre=head;
            while(head!=null){
                //    如果存在,则更新value
                pre=head;
                if(head.key==key){
                    head.value=value;
                    return;
                }
                head=head.next;
            }
            //    不存在则尾插
            Node curNode=new Node(key,value);
            pre.next=curNode;
            CURSIZE+=1;
        }
        public void remove(K key){
            int index=getHash(key,hashTable.length);
            Node head=hashTable[index];
            Node pre=head;
            while(head!=null && head.key!=key){
                //    如果存在,则更新value
                pre=head;
                head=head.next;
            }
            if(head==null){
                throw new RuntimeException("Delete Error: Key Not Found\n");
            }
            else{
                pre.next=head.next;
                if(hashTable[index].next==null){
                    CURSIZE-=1;
                    ifSubCapacity();
                }
            }
        }
    
        public int size(){
            return CURSIZE;
        }
        //    扩容,插入元素前判断
        public void ifAddCapacity(){
            if(CURSIZE>= hashTable.length*FACTOR){
                CURSIZE=0;
                MyHashMap<Integer,Integer> map=new MyHashMap<>();
                Node[] newHashTable=new Node[hashTable.length<<1];
                int newLen= newHashTable.length;
                for(int i=0;i<newHashTable.length;i++){
                    newHashTable[i]=new Node<>(-1,0);
                }
                //    ReHash
                for(int i=0;i<hashTable.length;i++){
                    if(hashTable[i].next!=null){
                        int index=getHash((K) hashTable[i].next.key,newLen);
                        if(newHashTable[index].next==null){
                            CURSIZE+=1;
                        }
                        Node newHead=newHashTable[index];
                        Node oldHead=hashTable[i].next;
                        while(oldHead!=null){
                            newHead.next=oldHead;
                            oldHead=oldHead.next;
                            newHead=newHead.next;
                        }
                    }
                }
                //     copy newHashTable to origin table
                this.hashTable=new Node[newLen];
                for(int i=0;i<hashTable.length;i++){
                    hashTable[i]=new Node<>(-1,0);
                    if(newHashTable[i].next!=null){
                        Node newHead=newHashTable[i].next;
                        Node curHead=hashTable[i];
                        while(newHead!=null){
                            curHead.next=newHead;
                            curHead=curHead.next;
                            newHead=newHead.next;
                        }
                    }
                }
            }
        }
        //    缩容,删除元素后判断
        public void ifSubCapacity(){
            int newLen=hashTable.length>>1;
            if(CURSIZE<= newLen*FACTOR){
                CURSIZE=0;
                Node[] newHashTable=new Node[newLen];
                for(int i=0;i<newLen;i++){
                    newHashTable[i]=new Node<>(-1,0);
                }
                //    ReHash
                for(int i=0;i<hashTable.length;i++){
                    Node curNode=hashTable[i].next;
                    while(curNode!=null){
                        int index=getHash((K) curNode.key,newLen);
                        Node newHead=newHashTable[index];
                        if(newHead.next==null){
                            CURSIZE++;
                        }
                        Node pre=newHead;
                        while(newHead!=null){
                            pre=newHead;
                            newHead=newHead.next;
                        }
                        pre.next=curNode;
                        curNode=curNode.next;
                    }
                }
                //    copy newHashTable to origin table
                this.hashTable=new Node[newLen];
                for(int i=0;i<newLen;i++){
                    hashTable[i]=new Node<>(-1,0);
                    if(newHashTable[i].next!=null){
                        Node newHead=newHashTable[i].next;
                        Node curHead=hashTable[i];
                        while(newHead!=null){
                            curHead.next=newHead;
                            curHead=curHead.next;
                            newHead=newHead.next;
                        }
                    }
                }
    
            }
        }
    
    }
    

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值