自定义hash表实现员工保存

/**
 * @author chenyi
 * @Description 自定义hashtable保存员工信息
 * @date 2022/2/17 11:12
 */
public class MyHashtable {

    int capacity = 11;
    private Node[] data;

    public MyHashtable() {
        data = new Node[11];
    }

    private int hash(int id) {
        return id % capacity;
    }

    public void add(Employee employee) {
        int hashCode = hash(employee.getId());
        Node newNode = new Node(employee);
        Node head = data[hashCode];
        if (head == null) {
            data[hashCode] = newNode;
            return;
        }
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = newNode;
    }

    public Employee find(int id) {
        int hashCode = hash(id);
        Node curr = data[hashCode];
        while (curr != null) {
            if (curr.getEmployee()
                    .getId() == id) {
                return curr.getEmployee();
            }
            curr = curr.next;
        }
        return null;
    }

    public void list() {
        for (int i = 0; i < data.length; i++) {
            Node curr = data[i];
            System.out.print("第" + (i + 1) + "条链表:");
            while (curr != null) {
                System.out.print(curr.getEmployee()
                                     .toString());
                curr = curr.next;
            }
            System.out.println();
        }
    }

    private Employee remove(int id) {
        int hashCode = hash(id);
        Node curr = data[hashCode];
        Node prev = null;
        while (curr != null) {
            if (curr.getEmployee()
                    .getId() == id) {
                if(prev==null) {
                    data[hashCode] = curr.next;
                }else{
                    prev.next = curr.next;
                }
                curr.next = null;
                return curr.employee;
            }
            prev = curr;
            curr = curr.next;
        }
        return null;
    }


    class Node {
        private Employee employee;
        private Node next;

        public Node(Employee employee) {
            this.employee = employee;
        }
        public Node getNext() {
            return next;
        }
        public void setNext(Node next) {
            this.next = next;
        }
        public Employee getEmployee() {
            return employee;
        }

    }

    public static void main(String[] args) {
        MyHashtable myHashtable = new MyHashtable();
        // 添加员工
        System.out.println("添加测试");
        myHashtable.add(new Employee(0,"jet"));
        myHashtable.add(new Employee(1,"jack"));
        myHashtable.add(new Employee(2,"smith"));
        myHashtable.add(new Employee(3,"jane"));
        myHashtable.add(new Employee(4,"jerry"));
        myHashtable.add(new Employee(11,"hover"));
        myHashtable.list();
        System.out.println("删除测试");
        myHashtable.remove(0);
        myHashtable.list();
        myHashtable.remove(0);
        myHashtable.list();
        myHashtable.remove(11);
        myHashtable.list();
        System.out.println("查询测试");
        Employee employee = myHashtable.find(4);
        System.out.println(employee);
    }

}

 class Employee {
    private int id;
    private String name;
    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "{" + "id=" + id + ", name='" + name + '\'' + '}';
    }


}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容