Hash table of data structure

Keywords: Algorithm data structure Cache

1, Basic introduction

1. Basic introduction
Hash table (also known as hash table) is a data structure that is accessed directly according to the Key value. In other words, it accesses records by mapping key values to a position in the table to speed up the search. This mapping function is called hash function, and the array storing records is called hash table.

2. Table implementation
Array ~ ~ ~ in the form of linked list

3. Common points of hash table

The program we developed will use java code to operate the database, but the performance of frequently operating the database is also very low. Therefore, we have common Redis Memcahe products, but the cache layer is generally written in Hash. We will try to write a cache layer ourselves.

2, Case study of hash table

1. Take a look at this topic first

2. Above problem analysis using hash table

(1) Emp class representing an employee

class Emp {
	private int id;
	private String name;
	private String address;
}

(2) Class representing the node

class EmpLinkedList {
	Emp head = null;//Head pointer, the previous linked list. If you haven't studied it, go to my home page to review it and point to the first employee in the current linked list
	//Kendy has methods such as add list
}

(3) Class representing our hash table

class HashTable {
	EmplinkedList[] empl;//Such an array
	//This calls add or list in EmpLikedList, but it must have a hash function (to determine which linked list the id corresponds to)
}

3. Code implementation

(1) Pit version

Pay attention to our construction method of new HashTab. There will be a pit in it

package cn.mldn;

import java.util.Arrays;

public class HashTabDemo {
    public static void main(String[] args) {
        HashTab hashTab = new HashTab(7);
        hashTab.add(new Emp(1,"Zheng"));
        hashTab.add(new Emp(2,"world"));
        hashTab.list();
    }
}

//Write hash table
class HashTab {//Manage multiple linked lists
    private EmpLinkedList[] empLinkedListArray;
    private int size;//Indicates how many linked lists there are

    //Instantiation, we'll take it to the constructor
    public HashTab(int size) {
        this.size = size;
        this.empLinkedListArray = new EmpLinkedList[size];
    }

    //Add employee
    public void add(Emp emp) {
        //According to the employee id, you can get which linked list the employee should join, so write a hash function
        int empLinkedListNo = hashFun(emp.id);
        //Add emp to the corresponding linked list
        empLinkedListArray[empLinkedListNo].add(emp);
    }

    //Traversal method, traversal hash table
    public void list() {
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i].list();
        }
    }
    //Hash function. There are many ways to write hash function. Here we use the modular method
    public int hashFun(int id) {
        return id % size;
    }
}


//Class representing an employee
class Emp {
    public int id;//Don't use public in reality
    public String name;
    public Emp next;//The default is null
    public Emp(int id,String name) {
        super();
        this.id = id;
        this.name = name;
    }
}


//Represents a linked list, which will store our data
class EmpLinkedList {
    //The header pointer executes the first Emp, so our linked list directly points to the first Emp
    private Emp head;//The default is empty

    //How to add employees
    //1. Adding employees is adding to the end
    //2. id is self increasing
    public void add(Emp emp) {
        //If you add the first employee
        if (head == null) {
            this.head = emp;
            return;
        }
        //If it is not the first employee, locate it to the last through an auxiliary variable
        Emp current = head;
        while (true) {
            if (current.next == null) {//It's the end
                break;
            }
            //Otherwise, move back one
            current = current.next;
        }
        //When you exit, it indicates that you have found it and hang it directly to the end
        current.next = emp;
    }


    //2. Traversal method
    public void list() {
        if (head == null) {//Description the linked list is empty
            System.out.println("The linked list is empty");
            return;
        }
        //Otherwise print
        System.out.println("The information of the current linked list is");
        Emp current = head;//Auxiliary pointer
        while (true) {
            System.out.printf("id = %d,name = %s\t",current.id,current.name);
            if (current.next == null) {//Description is the last node
                break;
            }
            current = current.next;//Backward traversal
        }
    }
}


The reason for this error is: when we create an array, the default value of the input array is zero, but the linked list employedlist class needs to be placed in our array, so we need to modify it here

(2) No pit version

package cn.mldn;

import java.util.Arrays;

public class HashTabDemo {
    public static void main(String[] args) {
        HashTab hashTab = new HashTab(7);
        hashTab.add(new Emp(1,"Zheng"));
        hashTab.add(new Emp(2,"world"));
        hashTab.list();
    }
}

//Write hash table
class HashTab {//Manage multiple linked lists
    private EmpLinkedList[] empLinkedListArray;
    private int size;//Indicates how many linked lists there are

    //Instantiation, we'll take it to the constructor
    public HashTab(int size) {
        this.size = size;
        this.empLinkedListArray = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i] = new EmpLinkedList();
        }
    }

    //Add employee
    public void add(Emp emp) {
        //According to the employee id, you can get which linked list the employee should join, so write a hash function
        int empLinkedListNo = hashFun(emp.id);
        //Add emp to the corresponding linked list
        empLinkedListArray[empLinkedListNo].add(emp);
    }

    //Traversal method, traversal hash table
    public void list() {
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i].list();
        }
    }
    //Hash function. There are many ways to write hash function. Here we use the modular method
    public int hashFun(int id) {
        return id % size;
    }
}


//Class representing an employee
class Emp {
    public int id;//Don't use public in reality
    public String name;
    public Emp next;//The default is null
    public Emp(int id,String name) {
        super();
        this.id = id;
        this.name = name;
    }
}


//Represents a linked list, which will store our data
class EmpLinkedList {
    //The header pointer executes the first Emp, so our linked list directly points to the first Emp
    private Emp head;//The default is empty

    //How to add employees
    //1. Adding employees is adding to the end
    //2. id is self increasing
    public void add(Emp emp) {
        //If you add the first employee
        if (head == null) {
            this.head = emp;
            return;
        }
        //If it is not the first employee, locate it to the last through an auxiliary variable
        Emp current = head;
        while (true) {
            if (current.next == null) {//It's the end
                break;
            }
            //Otherwise, move back one
            current = current.next;
        }
        //When you exit, it indicates that you have found it and hang it directly to the end
        current.next = emp;
    }


    //2. Traversal method
    public void list() {
        if (head == null) {//Description the linked list is empty
            System.out.println("The linked list is empty");
            return;
        }
        //Otherwise print
        System.out.println("The information of the current linked list is");
        Emp current = head;//Auxiliary pointer
        while (true) {
            System.out.printf("id = %d,name = %s\t",current.id,current.name);
            if (current.next == null) {//Description is the last node
                break;
            }
            current = current.next;//Backward traversal
        }
    }
}

(3) Analyze it


I add this: but the linked list is empty. Why is there such information? Because our data addition numbers are 1 and 2, it means that there is no data on the first linked list, there is data on the second and third linked lists, and there is no data below

(4) Full implementation

There is no way to delete it. You can practice it yourself

package cn.mldn;

import java.util.Arrays;

public class HashTabDemo {
    public static void main(String[] args) {
        HashTab hashTab = new HashTab(7);
        hashTab.add(new Emp(1,"Zheng"));
        hashTab.add(new Emp(2,"world"));
        hashTab.list();
        hashTab.findEmpById(1);
        hashTab.findEmpById(5);
    }
}

//Write hash table
class HashTab {//Manage multiple linked lists
    private EmpLinkedList[] empLinkedListArray;
    private int size;//Indicates how many linked lists there are

    //Instantiation, we'll take it to the constructor
    public HashTab(int size) {
        this.size = size;
        this.empLinkedListArray = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i] = new EmpLinkedList();
        }
    }

    //Add employee
    public void add(Emp emp) {
        //According to the employee id, you can get which linked list the employee should join, so write a hash function
        int empLinkedListNo = hashFun(emp.id);
        //Add emp to the corresponding linked list
        empLinkedListArray[empLinkedListNo].add(emp);
    }

    //Traversal method, traversal hash table
    public void list() {
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i].list();
        }
    }
    //Hash function. There are many ways to write hash function. Here we use the modular method
    public int hashFun(int id) {
        return id % size;
    }

    //Search method
    public void findEmpById(int id) {
        int findId = hashFun(id);
        Emp empById = empLinkedListArray[findId].findEmpById(id);
        if (empById != null) {
            System.out.println("stay" + id + "Linked list found");
            System.out.println();
        } else {
            System.out.println("Can't find");
        }
    }
}


//Class representing an employee
class Emp {
    public int id;//Don't use public in reality
    public String name;
    public Emp next;//The default is null
    public Emp(int id,String name) {
        super();
        this.id = id;
        this.name = name;
    }
}


//Represents a linked list, which will store our data
class EmpLinkedList {
    //The header pointer executes the first Emp, so our linked list directly points to the first Emp
    private Emp head;//The default is empty

    //How to add employees
    //1. Adding employees is adding to the end
    //2. id is self increasing
    public void add(Emp emp) {
        //If you add the first employee
        if (head == null) {
            this.head = emp;
            return;
        }
        //If it is not the first employee, locate it to the last through an auxiliary variable
        Emp current = head;
        while (true) {
            if (current.next == null) {//It's the end
                break;
            }
            //Otherwise, move back one
            current = current.next;
        }
        //When you exit, it indicates that you have found it and hang it directly to the end
        current.next = emp;
    }


    //2. Traversal method
    public void list() {
        if (head == null) {//Description the linked list is empty
            System.out.println("The linked list is empty");
            return;
        }
        //Otherwise print
        System.out.println("The information of the current linked list is");
        Emp current = head;//Auxiliary pointer
        while (true) {
            System.out.printf("id = %d,name = %s\t",current.id,current.name);
            if (current.next == null) {//Description is the last node
                break;
            }
            current = current.next;//Backward traversal
        }
    }

    //3. Search method
    public Emp findEmpById(int id) {
        //Judge whether the linked list is empty
        if (head == null) {
            System.out.println("The linked list is empty");
            return null;
        }
        //Auxiliary pointer
        Emp current = head;
        while (true) {
            if (current.id == id) {//eureka
                System.out.printf("id = %d,name = %s\t",current.id,current.name);
                break;//That means it's this man
            }
            if (current.next == null) {//It means you haven't found the person you want
                current = null;
                break;
            }
            //Otherwise you have to move a position
            current = current.next;

        }
        return current;
    }
}

Posted by thenature4u on Thu, 28 Oct 2021 20:36:02 -0700