- 实现一个简单的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; } } } } } }
手撕HashMap
于 2024-04-28 20:14:56 首次发布