哈希表

哈希表的原理:
一个数组 + 数组中每条数据都是一个单向链表

哈希表_第1张图片

package com.cyber;

/**
 * 哈希表Demo
 */
public class HashTabDemo{
    public static void main(String args[]){
        HashTab hashTab = new HashTab(7);
        hashTab.add(new Person(0,"AAA"));
        hashTab.add(new Person(1,"BBB"));
        hashTab.add(new Person(2,"CCC"));
        hashTab.add(new Person(3,"DDD"));
        hashTab.add(new Person(4,"EEE"));
        hashTab.add(new Person(5,"FFF"));
        hashTab.add(new Person(6,"GGG"));
        hashTab.add(new Person(7,"HHH"));
        hashTab.add(new Person(8,"III"));
        hashTab.list();
        hashTab.findById(5);
    }
}

class HashTab{

    private PersonLinkedList[] personLinkedListArray;
    private int size;
    public HashTab(int size){
        this.size = size;
        personLinkedListArray = new PersonLinkedList[size];
        for(int i=0;i<size;i++){
            PersonLinkedList personLinkedList = new PersonLinkedList();
            personLinkedListArray[i] = personLinkedList;
        }
    }

    public void findById(int id){
        int index = getArrayIndex(id);
        personLinkedListArray[index].findById(id);
    }


    public void add(Person person){
        int index = getArrayIndex(person.id);
        personLinkedListArray[index].add(person);
    }

    public void list(){
        for(int i=0;i<size;i++){
            personLinkedListArray[i].list(i);
        }
    }
    public int getArrayIndex(int id){
        return id % size;
    }

}

class PersonLinkedList{

    private Person head;

    public void add(Person person){
        
        if(head == null){
            head = person;
            return ;
        }else{
            Person current = head;
            while(true){
                if(current.next == null){
                    current.next = person;
                    return;
                }else{
                    current = current.next;
                }
            }


        }
    }

    public void list(int index){
        if(head == null){
            System.out.println("当前链表为空");
        }else{
            Person person = head;
            System.out.println("第"+index+"个链表");
            while(true){
                System.out.println(person.toString());
                if(person.next !=null){
                    person = person.next;
                }else{
                    break;
                }
            }
        }
    }

    public void findById(int id){
        if(head == null){
            System.out.println("查无此人");
        }else{
            Person person = head;
            while(true){
                if(person.id == id){
                    System.out.println(person.toString());
                    break;
                }else{
                    if(person.next == null){
                        System.out.println("查无此人");
                        break;
                    }else{
                        person = person.next;
                    }
                }
            }
        }
    }
    
}

class Person{
    public int id;
    public String name;
    public Person next;
    public Person(int id,String name){
        this.id=id;
        this.name=name;
    }
    public String toString(){
        System.out.println("id="+id+",name="+name);
        return "";
    }
}

你可能感兴趣的:(哈希表)