JAVA basic arrangement

HashMap learning -- write a simple HashMap manually for understanding

1. Definition of Node

public class Node {
    public int hash;
    public Object key;
    public Object value;
    public Node next;
}

The basis of HashMap is an array. Each element in the array is a node. It must include: hash value, key value, value, next node. Nodes with the same hash value are chained together.

2. Definition of HashMap

Node[] table;//Core array
    int size;//Element number
    
     public HashmapTest(){
        table=new Node[16];//Initialize the array size to an integer power of 2
        size=0;
    }

The size of this array is exquisite. From the two extremes, when the size is 1, that is, all elements are tied to one place, that is, it degenerates into a linked list. If the array is large, that is, all elements can be scattered in almost every bit of the array, that is to say, it degenerates into an array. As for taking the integer power of 2, you can use bitwise &, to speed up the speed, and the speed of division in the computer is very slow.

3. Addition, deletion, modification and query of HashMap

Add an element

 public void put(K key,V value){
        Node newNode=new Node();
        Node lastNode=null;
        Boolean isRepeat=false;
        newNode.hash=myhash(key.hashCode(),table.length);//A simple function to calculate the hash value is to take the remainder of the array size without considering the expansion and optimization
        newNode.key=key;
        newNode.value=value;
        newNode.next=null;
        if(table[newNode.hash]==null){
            table[newNode.hash]=newNode;
        }else{
            Node tmp=table[newNode.hash];
            while (tmp!=null){
                if(tmp.key.equals(key)){
                    isRepeat=true;
                    tmp.value=value;
                    break;
                }else{
                    lastNode=tmp;
                    tmp=tmp.next;
                }

            }
            if(isRepeat==false){
                lastNode.next=newNode;
            }

        }

    }

Delete the element corresponding to the key value

 public void delete(K key){
        int hash=myhash(key.hashCode(),table.length);
        Node temp=table[hash];
        Node prevNode=null;//Remember the previous node and connect it when deleting.
        while(temp!=null){
            //Special operation of the first node
            if(temp.key.equals(table[hash].key)){
                //The first one is right
                if(temp.key.equals(key)){
                     table[hash]=temp.next;
                    break;
                }else{
                    prevNode=temp;
                    temp=temp.next;
                }
            }else{
                if(temp.key.equals(key)){
                    prevNode.next=temp.next;
                    break;
                }else{
                    temp=temp.next;
                }

            }
        }
    }

Modify an element

public void set(K key,V value){
        Node temp=table[myhash(key.hashCode(),table.length)];
        while (temp!=null){
            if(temp.key.equals(key)){
                temp.value=value;
                break;
            }else {
                temp=temp.next;
            }
        }
    }

Find an element

 public V get(K key){
        int hash=myhash(key.hashCode(),table.length);
        Node tmp=table[hash];
        if(tmp==null){
            return null;
        }else{
            while (tmp!=null){
                if(tmp.key.equals(key)){
                    return (V)tmp.value;
                }else {
                    tmp=tmp.next;
                }
            }
            return null;
        }
    }

Tags: Java

Posted on Thu, 07 Nov 2019 12:55:48 -0500 by phpdragon