Java implements hash table (hash)

1 Introduction

Hash table (Hash table, also called hash table) is a data structure that is directly accessed according to the key value (Key value). That is, it accesses records by mapping key values ​​to a location in the table to speed up lookups. This mapping function is called a hash function, and the array storing the records is called a hash table.
Given a table M, there is a function f(key). For any given key value key, if the address of the record in the table containing the key can be obtained after substituting the function, the table M is called a hash (Hash) table, the function f(key) is a hash (Hash) function.

  • Insert a keyword into the hash table: the hash function determines which block in the table the corresponding value of the keyword should be stored in, and puts the keyword after the last node of the linked list in the block
  • Search for a keyword in the hash table: use the same hash function to find the corresponding block from the hash table, and search for the value corresponding to the keyword in a specific block

A hash table is a data structure that combines an array with a linked list

2. Thinking analysis

  • Need a node class, a linked list, an array
  • A linked list is stored in the array, and a node is stored in the linked list

3. Diagram

insert image description here

The form of array + linked list

4. Code implementation


/**
 * 简单实现hash表
 * @author 尹稳健~
 * @version 1.0
 * @time 2022/9/24
 */
public class HashTabExap {
    
    
    public static void main(String[] args) {
    
    
        HashTab hashTab = new HashTab(5);
        Person person1 = new Person(1,"张三");
        Person person2 = new Person(2,"李四");
        Person person3 = new Person(3,"王五");
        Person person4 = new Person(4,"赵六");
        Person person5 = new Person(5,"何招财");
        Person person6 = new Person(6,"冰有茂");
        hashTab.add(person1);
        hashTab.add(person2);
        hashTab.add(person3);
        hashTab.add(person4);
        hashTab.add(person5);
        hashTab.add(person6);
        hashTab.traverse();

    }


}

/** 节点 */
class Person{
    
    
    int id;
    String name;
    Person next;

    public Person(int id, String name) {
    
    
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

class PersonLinkedList{
    
    
    /** 头节点 */
    private Person personHead = new Person(-1,"");

    /**
     * 向链表中添加节点
     * @param person
     */
    public void addPersonNode(Person person){
    
    
        // 头节点的下一个节点如果为空那么就是空链表,直接添加到头节点的后面
        Person tempPersonNode = personHead.next;
        if (tempPersonNode == null){
    
    
            personHead.next = person;
            return;
        }
        // 遍历找到最后一个节点,将元素添加到最后一个节点后面
        while (tempPersonNode.next != null){
    
    
            tempPersonNode = tempPersonNode.next;
        }
        tempPersonNode.next = person;
    }

    /**
     * 遍历链表中的所有节点元素
     */
    public void traverse(){
    
    
        Person tempPersonNode = personHead.next;
        if (tempPersonNode == null){
    
    
            System.out.println("链表为空!");
            System.out.println("=========");
            return;
        }
        while (tempPersonNode!=null){
    
    
            System.out.println(tempPersonNode);
            tempPersonNode = tempPersonNode.next;
        }
        System.out.println("=============");
    }

    /**
     * 根据id搜索节点元素
     * @param id
     */
    public void findNodeById(int id){
    
    
        if (personHead.next == null){
    
    
            System.out.println("链表为空!");
            return;
        }
        Person tempPersonNode = personHead.next;
        while (tempPersonNode.next != null){
    
    
            if (tempPersonNode.id == id){
    
    
                System.out.println(tempPersonNode);
                return;
            }
            tempPersonNode = tempPersonNode.next;
        }
        System.out.println("未找到!");
    }


}

class HashTab{
    
    
    private PersonLinkedList[] personLinkedLists;
    private final int size;

    public HashTab(int size) {
    
    
        this.size = size;
        personLinkedLists = new PersonLinkedList[size];
        // 初始化链表,不然会报空指针异常
        for (int i = 0; i < size; i++) {
    
    
            personLinkedLists[i] = new PersonLinkedList();
        }
    }

    /**
     * 向数组元素中元素,再在链表中添加元素
     * @param personNode
     */
    public void add(Person personNode){
    
    
        personLinkedLists[getHash(personNode.id)].addPersonNode(personNode);
    }

    /**
     * 遍历每个数组中的链表元素
     */
    public void traverse(){
    
    
        for (int i = 0; i < size; i++) {
    
    
            personLinkedLists[i].traverse();
        }
    }

    /**
     * 算出节点元素添加在数组的哪个位置,再添加在链表的最后
     * @param id
     * @return
     */
    public int getHash(int id){
    
    
        return id % size;
    }

    public void findNodeById(int id){
    
    
        int hash = getHash(id);
        personLinkedLists[hash].findNodeById(id);
    }
}

Guess you like

Origin blog.csdn.net/weixin_46073538/article/details/127024115