10 hash table
introduce:
In one company, when a new employee comes to report, it is required to add the employee's information (id, gender, age, address...). When entering the employee's id, it is required to find all the information of the employee.
Requirement: not applicable to data structure, try to save memory, the faster the better = > hash table (hash)
1) Basic introduction
Hash table (also known as hash table) is a data structure directly accessed according to key value.
In other words, it is accessed by mapping the key directly to the location in the table to speed up the search
This mapping function is called a hash function, and the array of records is called a Hash list
2) Hash table application scenario
A java program will often transfer the data in the database. In order to speed up this call, the programmer adds a cache layer between the Java program and the database to cache the data to be called and speed up the call in turn.
The cache layer can be existing cache products, such as Redis and Memcache; You can also write it yourself, such as a hash table.
3) Structure of hash table
1. There are two forms:
- Array + linked list
- Array + binary tree
2. Memory structure of hash table
As shown in the figure, it is in the form of array (left) + linked list (right)
4) Hash table implementation idea diagram
A computer problem of google
In one company, when a new employee comes to report, it is required to add the employee's information (id, gender, age, address...). When entering the employee's id, it is required to find all the information of the employee.
Requirement: not applicable to data structure, try to save memory, the faster the better = > hash table (hash)
When adding, ensure that the id is inserted from low to high
Question making process:
- A linked list is used to implement the hash table. The linked list does not take the lead (that is, the first node of the linked list stores employee information)
- Analyze ideas and draw schematic diagrams
- Code implementation ↓
5) Code implementation - addition, deletion, modification and query
package DataStructures.Hashtable; import java.util.Scanner; public class HashTableDemo { public static void main(String[] args) { //Create hash table HashTable hashTable = new HashTable(7); //Write a simple menu String key = ""; Scanner scanner = new Scanner(System.in); while (true){ System.out.println("add: Add employee"); System.out.println("list: Show employees"); System.out.println("find: Find employees"); System.out.println("exit: Exit the system"); System.out.println("del: Delete employee"); key = scanner.next(); switch (key){ case "add": System.out.println("input id"); int id = scanner.nextInt(); System.out.println("Enter name"); String name = scanner.next(); Emp emp = new Emp(id,name); hashTable.add(emp); break; case "list": hashTable.list(); break; case "find": System.out.println("Please enter the to find id"); id = scanner.nextInt(); hashTable.findEmpById(id); break; case "del": System.out.println("Please enter the to delete id"); id = scanner.nextInt(); hashTable.delEmp(id); break; case "exit": scanner.close(); System.exit(0); break; default: break; } } } } //Create a HashTable to manage multiple linked lists class HashTable { private EmpLinkedList empLinkedListArray[]; private int size; //constructor public HashTable(int size) { this.size = size; empLinkedListArray = new EmpLinkedList[size]; //Leave a hole = > don't forget to initialize each linked list separately at this time for (int i=0;i<size;i++){ empLinkedListArray[i] = new EmpLinkedList(); } } //Add employee public void add(Emp emp) { //The linked list to which the employee should be added is obtained according to the employee's id //So write a hash function ↓ int empLinkedListNO = hashFun(emp.id); //Add emp to the corresponding linked list empLinkedListArray[empLinkedListNO].add(emp); } //Traverse all linked lists and hashtabl e public void list() { for (int i = 0; i < size; i++) { empLinkedListArray[i].list(i); } } //Find employees based on the id entered public void findEmpById(int id){ //Use the hash function to determine which linked list to look up int empLinkedListNo = hashFun(id); Emp emp = empLinkedListArray[empLinkedListNo].findEmpById(id); if(emp!=null){ //find System.out.printf("In the first%d Employee found in linked list id = %d\n",(empLinkedListNo+1),id); }else { System.out.println("The employee was not found in the hash table"); } } //Delete the employee information corresponding to the entered id public void delEmp(int id){ int empLinkedListNo = hashFun(id); empLinkedListArray[empLinkedListNo].del(id); } //Write hash functions, using a simple modular method public int hashFun(int id) { return id % size; } } //Represents an employee class Emp { public int id; public String name; public Emp next; public Emp(int id, String name) { super(); this.id = id; this.name = name; } } //Create an EmpLinkedList to represent the linked list class EmpLinkedList { //The header pointer executes the first Emp, so the head of our linked list directly points to the first Emp private Emp head;//The default is null //Add employee to linked list //explain //1. Suppose that adding an employee is the end of the linked list public void add(Emp emp) { //If you are adding the first employee if (head == null) { head = emp; return; } //There it is! Auxiliary pointer that must appear in the linked list Emp curEmp = head; while (true) { if (curEmp.next == null) { break; } curEmp = curEmp.next; } //When exiting, add emp directly to the linked list curEmp.next = emp; } //Employee information traversing the linked list public void list(int no) { if (head == null) { System.out.println("The first"+(no+1)+"The linked list is empty"); return; } System.out.println("The first"+(no+1)+"The linked list information is:"); Emp curEmp = head; while (true) { System.out.printf(" =>id=%d name=%s\t", curEmp.id, curEmp.name); if (curEmp.next == null) { break; } curEmp = curEmp.next; } System.out.println(); } //Find employees by id //If found, return Emp; if not, return null public Emp findEmpById(int id){ //Determine whether the linked list is empty if(head==null){ System.out.println("The linked list is empty"); return null; } Emp curEmp = head; while (true){ if(curEmp.id==id){ break; } //Exit condition if (curEmp.next==null){ //no find curEmp = null; break; } curEmp = curEmp.next; } return curEmp; } public void del(int id){ if(head==null){ System.out.println("The linked list is empty and cannot be deleted"); return; } if (head.id==id){ if(head.next==null){ head=null; }else { head = head.next; } return; } Emp curEmp = head; boolean flag = false; while (true){ if(curEmp.next==null){ break; } if (curEmp.next.id==id){ flag=true; break; } curEmp = curEmp.next; } if (flag){ curEmp.next = curEmp.next.next; }else { System.out.println("The node to be deleted does not exist"); } } }
notes: First, the supervision node Emp Then establish a single headless linked list EmpLinkedList Finally, it is converted into a hash table in the form of a linked list HashTable(That is, a linked list represented by an array) See the illustration above for details