자바 자료구조

☕️ [JAVA] 해시리니어 구현

King of Silicon Valley 2021. 10. 7. 18:21
728x90

해시 자료구조중 충돌이날 경우 다음 인덱스에 저장을하는 로직으로 구현해 보았다. 

 

public class HashLinear {

    public Bucket[] hashTable;

    public HashLinear(Integer size){
        this.hashTable = new Bucket[size];
    }

    public class Bucket{
        String key;
        String value;

        Bucket(String key, String value) {
            this.key = key;
            this.value = value;
        }
    }

    public int hashFunc(String key) {
        return (int) key.charAt(0) % 20;
    }

    public boolean saveData(String key, String value) {
        int address = hashFunc(key);
        if (this.hashTable[address] == null) {
            this.hashTable[address] = new Bucket(key, value);
            return true;
        } else {
            if (this.hashTable[address].key == key) {
                this.hashTable[address].value = value;
                return true;
            } else {
                int memoryAddress = address;
                while (memoryAddress < 19) {
                    memoryAddress++;
                    if (this.hashTable[memoryAddress] == null) {
                        this.hashTable[memoryAddress] = new Bucket(key, value);
                        return true;
                    }
                }
                for (int i=0;i < address ;i++){
                    if (this.hashTable[i] == null) {
                        this.hashTable[i] = new Bucket(key, value);
                        return true;
                    }
                }
                return false;
            }
        }
    }

    public String getData(String key) {
        int address = hashFunc(key);
        if (this.hashTable[address] == null) {
            return null;
        } else {
            if (this.hashTable[address].key == key) {
                return this.hashTable[address].value;
            }
            int memoryAddress = address;
            while (memoryAddress < 19) {
                memoryAddress++;
                if (this.hashTable[memoryAddress] != null && this.hashTable[memoryAddress].key == key) {
                    return this.hashTable[memoryAddress].value;
                }
            }
            for (int i=0;i < address ;i++){
                if (this.hashTable[i] != null && this.hashTable[i].key == key) {
                    return this.hashTable[i].value;
                }
            }
            return null;
        }
    }

    public static void main(String[] args) {
        HashLinear myHash = new HashLinear(20);
        System.out.println(myHash.hashFunc("wefew"));
        System.out.println(myHash.saveData("팔리", "123"));
        System.out.println(myHash.saveData("팔리0", "123"));
        System.out.println(myHash.saveData("팔리1", "123"));
        System.out.println(myHash.saveData("팔리2", "123"));
        System.out.println(myHash.getData("팔리"));
        System.out.println(myHash.getData("팔리0"));
        System.out.println(myHash.getData("팔리1"));
        System.out.println(myHash.getData("팔리2"));
    }
}

'자바 자료구조' 카테고리의 다른 글

☕️ [JAVA] ArrayList  (0) 2021.10.12
☕️ [JAVA] 이진 탐색트리 구현  (0) 2021.10.08
☕️ [JAVA] 다중 연결리스트 구현  (0) 2021.10.06
☕️ [JAVA] 연결 리스트 구현  (0) 2021.10.06
💽 자바 배열  (0) 2021.10.05