Data structure: Hash list implementation ideas and examples

Posted by Stu on Mon, 07 Feb 2022 18:41:25 +0100


Here's a little cold blog
_Column on High Quality Technology and Good Writing
Personal Public Number, share some technical articles, and pits encountered
Current Series: Data Structure Series
Source code git repository'
Data structure code address Code Git repository address


A basic introduction to hash tables

A Hash table is a data structure that is directly accessed based on a key value. That is, it accesses records by mapping key code values to a 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.

Technical Prospect: How to Solve Without Caching Products

Hash list after graphical implementation

The idea is to use arrays as a unique identifier for mapping, and the index in each array drinks a list of chains

Examples of department numbers can be interpreted as values for arrays

Department number: Name (value saved in the list)

google Topic

There is a company that asks for information about a new employee (id, gender, age, name, address...) to be added when a new employee reports. When an employee's ID is entered, all information about the employee is required to be found.


  1. Without a database, the faster the better=>hash table (hash)

  2. When adding, make sure to insert id from low to high after class thinking: If id is not insert from low to high, but requires each chain list to be from low to high, how to solve?

  3. Use a chain table to implement a hash table without a header [that is, the first node of the chain table stores employee information

Ideas analyze and draw diagrams

Idea Realization

 * @projectName: DataStructure
 * @package: com.hyc.DataStructure.hashtable
 * @className: hashtebDemo
 * @author: Cold ring doomwatcher
 * @description: TODO
 * @date: 2022/2/1 3:01
 * @version: 1.0
public class hashtebDemo {
    public static void main(String[] args) {

        //Create a hash table
        hashtable hashTab = new hashtable(7);

        //Write a simple menu
        String key = "";
        Scanner scanner = new Scanner(;
        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 System");

            key =;
            switch (key) {
                case "add":
                    System.out.println("input id");
                    int id = scanner.nextInt();
                    System.out.println("Enter a name");
                    String name =;
                    //Create Employee
                    Emp emp = new Emp(id, name);
                case "list":
                case "find":
                    System.out.println("Please enter what you are looking for id");
                    id = scanner.nextInt();
                case "exit":



//There are two ways to create a hashtab to manage multiple linked lists, that is, to map with arrays
//1. Array + Chain List
//2. Binary Tree
class hashtable {
    //Array for mapping a list of chains
    private EmpLinkedList[] empLinkedListArrays;
    // Used to record length
    private int size;
//    Constructor Initialization

    public hashtable(int size) {
        this.size = size;
        //    Initialize Emplinkedlistarrays
        empLinkedListArrays = new EmpLinkedList[size];
        //   Don't initialize each list separately at this time. We initialize it all at once
        for (int i = 0; i < size; i++) {
            empLinkedListArrays[i] = new EmpLinkedList();

    //    Add Members
    public void add(Emp emp) {
        //  Get the list that the employee should add based on the employee id
        int emplinkedlistNo = hashFun(;
        //   Add emp to the corresponding list

    //Traverse through all linked lists Traverse hashtab
    public void list() {
        for (int i = 0; i < size; i++) {

    //Find id
    public void findEmpById(int id) {
        //       Use the hash function to determine which chain list to go to
        int empLinkedListNo = hashFun(id);
        Emp empByid = empLinkedListArrays[empLinkedListNo].findEmpByid(id);
        if (empByid == null) {
            System.out.println("No employees found");
        } else {
            System.out.println("stay" + (empLinkedListNo + 1) + "Employees found in the list,id by=>" +;

    //    Use modularization to calculate the id array of the corresponding mapping of the chain table based on the id
    public int hashFun(int id) {
        return id % size;

class Emp {
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) { = id; = name;

class EmpLinkedList {
    //Header pointer, executes the first emp so the head er of our list is directly pointing to the first emp
    private Emp head;

    //    Add Employees to List
//    1. Assuming that when you add an employee, the id always increases from small to large, so we don't need to handle the judgment of whether the ID is in order.
//    Add directly to the end of the current list
    public void add(Emp emp) {
        //If the header node is empty, add it because it is the first employee
        if (head == null) {
            head = emp;
        //    If the header node is not empty that means it is not the first one, we need a pointer to the header node
        Emp curEmp = head;
        //   Loop the list to the end
        while (true) {
            //If you enter this if then the list is at the end
            if (curEmp == null) {
            //Each loop moves curEmp back until it triggers if to break
            curEmp =;
        //   Exiting the loop means curEmp is the last one, so here we'll point next to emp for the parameter = emp;

    //  Traverse a list of chains, the parameter is used to determine which one belongs to
    public void list(int no) {
        if (head == null) {
            //   Entering this judgment indicates that the chain list is empty
            System.out.println("No." + (no + 1) + "Chain list is empty");
        System.out.print("No." + (no + 1) + "The current list information is");
        Emp curEmp = head;
        while (true) {
            if ( == null) {
            curEmp =;

    //Find a list of chains based on id
    public Emp findEmpByid(int id) {
        if (head == null) {
            System.out.println("The list is empty");
            return null;
        Emp curemp = head;
        while (true) {
            if ( == id) {
            if ( == null) {
                System.out.println("There are no employees to look for in this list");
            //Move back
            curemp =;
        return curemp;

Effect demonstration

New and Traversal


Topics: Java Algorithm data structure