public class MyHashMap { private final int N=10000; private Node[] arr; public MyHashMap(){ arr=new Node[N]; } public static void main(String[] args) { MyHashMap hm=new MyHashMap(); Hm. Put (1, 1); Hm. Put (2, 2); hm.remove(1); System.out.println(hm.get(1)); } public int get(int key){ int idx=hash(key); if(arr[idx]! =null){ Node cur=arr[idx].next; while (cur! =null){ if(cur.key==key){ return cur.value; } cur=cur.next; } } return -1; } public void remove(int key){ int idx=hash(key); if(arr[idx]! =null){ Node prev=arr[idx]; while(prev.next! =null){ if(prev.next.key==key){ Node delNode=prev.next; prev.next=delNode.next; delNode.next=null; return; } prev=prev.next; } } } public void put(int key,int value){ int idx=hash(key); if(arr[idx]==null){ arr[idx]=new Node(-1,-1); arr[idx].next=new Node(key,value); }else { while (arr[idx].next! =null){ if(arr[idx].next.key==key){ arr[idx].next.value=value; return; } arr[idx]=arr[idx].next; } arr[idx].next=new Node(key,value); } } private int hash(int key) { return key%N; } private class Node{ int key; int value; Node next; Node(int key,int value){ this.key=key; this.value=value; }}}Copy the code