해시 표 의 증가, 삭제, 조사, 옮 겨 다 니 기

6744 단어 데이터 구조
package com.zzb.hashtable;

import java.io.Serializable;

/**
 * @Auther: Administrator
 * @Date: 2019/10/7 00:44
 * @Description:      、 、 、  
 *
 *                                   , 
 *     =    +   
 *
 *      ,          ,           (id,  ,  ,  ,  ...),       id ,
 *              
 *
 *   :
 * (1)      ,,      =>   (   )
 * (2)   ,    id      (  id        ,         id      ,
 *             ,   https://blog.csdn.net/qq_16645099/article/details/101919137
 *               addByOrder)
 * (3)          ,        (                )
 */
public class HashTableDemo01 {
    public static void main(String[] args) {
        HashTable hashTable = new HashTable(7);
        //          
        hashTable.add(new Employee(1, "aa"));
        hashTable.add(new Employee(2, "bb"));
        hashTable.add(new Employee(3, "cc"));
        hashTable.add(new Employee(4, "dd"));
        hashTable.add(new Employee(5, "ee"));
        hashTable.add(new Employee(6, "ff"));
        hashTable.add(new Employee(7, "gg"));
        hashTable.add(new Employee(8, "hh"));
        hashTable.add(new Employee(9, "ii"));
        hashTable.add(new Employee(10, "jj"));
        //      
        hashTable.list();
        //          
        System.out.println("          " + hashTable.findOneById(10));

        /*----------------  1            ----------------
        Employee{id=7, name='gg'}
        ----------------  2            ----------------
        Employee{id=1, name='aa'}
        Employee{id=8, name='hh'}
        ----------------  3            ----------------
        Employee{id=2, name='bb'}
        Employee{id=9, name='ii'}
        ----------------  4            ----------------
        Employee{id=3, name='cc'}
        Employee{id=10, name='jj'}
        ----------------  5            ----------------
        Employee{id=4, name='dd'}
        ----------------  6            ----------------
        Employee{id=5, name='ee'}
        ----------------  7            ----------------
        Employee{id=6, name='ff'}
                  Employee{id=10, name='jj'}*/
    }
}

//        
class HashTable {
    private LinkedList[] array;
    //          
    private Integer size;

    public HashTable() {
        this(10);
    }

    //       ,       ,          
    public HashTable(Integer size) {
        this.size = size;
        array = new LinkedList[size];
        //              
        for (int i = 0; i < size; i++) {
            array[i] = new LinkedList();
        }
    }

    //         
    public void add(Employee employee) {
        //           
        Integer index = hashIndex(employee.getId());
        this.getArray()[index].add(employee);
    }

    //            
    public Employee findOneById(Integer id) {
        //           
        Integer index = hashIndex(id);
        return this.getArray()[index].findOneById(id);
    }

    //      
    public void list() {
        for (int i = 0; i < this.getSize(); i++) {
            this.getArray()[i].list(i);
        }
    }

    //     ,         ,     
    public Integer hashIndex(Integer id) {
        return id % this.getSize();
    }


    public LinkedList[] getArray() {
        return array;
    }

    public void setArray(LinkedList[] array) {
        this.array = array;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }
}

//          
class LinkedList {
    //    ,     employee,         head        employee
    private Employee head; //    null

    public Employee getHead() {
        return head;
    }

    public void setHead(Employee head) {
        this.head = head;
    }

    /**
     *     
     *   ,      ,id    , id         
     *                   
     * @param employee
     */
    public void add(Employee employee) {
        if(this.getHead() == null) { //         
            head = employee;
            return;
        } else { //         
            Employee temp = this.getHead();
            while(true) {
                if(temp.getNext() == null) { // temp next   null     
                    break;
                }
                //   
                temp = temp.getNext();
            }
            //     ,       
            temp.setNext(employee);
        }
    }

    //     
    public Employee findOneById(Integer id) {
        if(this.getHead() == null) {
            System.out.println("    !");
            return null;
        } else {
            Employee temp = this.getHead();
            while(true) {
                if(temp.getId() == id) {
                    break;
                } else {
                    if(temp.getNext() == null) { //                 
                        temp = null;
                        break;
                    }
                    //   
                    temp = temp.getNext();
                }
            }
            return temp;
        }
    }

    //     
    public void list(Integer index) {
        if(this.getHead() == null) {
            System.out.println("  " + (index+1) + "      !");
            return;
        } else {
            System.out.println("----------------  " + (index+1) + "            ----------------");
            Employee temp = this.getHead();
            while(true) {
                System.out.println(temp);
                if(temp.getNext() == null) { //           
                    break;
                } else {
                    temp = temp.getNext();
                }
            }
        }
    }
}

//      
class Employee implements Serializable {

    private static final long serialVersionUID = 3094191959099739635L;

    private Integer id;
    private String name;
    private Employee next; //           ,    

    public Employee() {

    }

    public Employee(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Employee getNext() {
        return next;
    }

    public void setNext(Employee next) {
        this.next = next;
    }

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

좋은 웹페이지 즐겨찾기