본문 바로가기

IT/자료구조 & 알고리즘

[자료구조] HashTable Source

import java.util.LinkedList;

public class HashTable {

    //링크드리스트를 저장하는 배열
    private LinkedList<Node>[] data;

    // 데이터를 key, value로 저장하는 Node
    class Node {
        String key;
        String value;

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

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    //hashtable의 크기를 미리 선언해둔다
    public HashTable(int size) {
        this.data = new LinkedList[size];
    }

    //해시 코드를 key의 각 아스키코드를 더해서 반환한다.
    private int getHashCode(String key) {
        int hashcode = 0;
        for (char c : key.toCharArray()) {
            hashcode += c;
        }
        return hashcode;
    }

    //해쉬코드를 이용해서 index를 생성한다.
    private int convertToIndex(int hashcode) {
        return hashcode % data.length;
    }

    //특정 키를 이용해서 링크드리스트 내 node를 검색하는 메소드
    private Node searchNodeByKey(LinkedList<Node> list, String key) {

        if (list == null) {
            return null;
        }

        for (Node node : list) {
            if (node.key.equals(key)) {
                return node;
            }
        }
        return null;
    }

    //key의 value를 저장하는 메소드
    public void put(String key, String value) {
        int hashcode = getHashCode(key);
        int index = convertToIndex(hashcode);
        LinkedList<Node> list = data[index];

        if (list == null) {
            list = new LinkedList<>();
            data[index] = list;
        }

        Node node = searchNodeByKey(list, key);

        if (node == null) {
            node = new Node(key, value);
            System.out.println("key : " + key + ", hashcode : " + hashcode + " index : " + index);
            list.addLast(node);
        } else {
            node.setValue(value);
        }
    }

    //특정 key를 이용해 value를 반환하는 메소드
    public String get(String key) {
        int hashcode = getHashCode(key);
        int index = convertToIndex(hashcode);
        LinkedList list = data[index];
        if (list == null) {
            return null;
        } else {
            Node node = searchNodeByKey(list, key);
            return node == null ? null : node.value;
        }
    }
}
public class Main {

    public static void main(String[] args) {

        HashTable hashTable = new HashTable(5);

        hashTable.put("kim","my name is kim");
        hashTable.put("park","my name is park");
        hashTable.put("ko","my name is ko");
        hashTable.put("lee","my name is lee");
        hashTable.put("lee","my name is lee lee");

        System.out.println(hashTable.get("kim"));
        System.out.println(hashTable.get("park"));
        System.out.println(hashTable.get("ko"));
        System.out.println(hashTable.get("lee"));
        System.out.println(hashTable.get("sang"));

    }
}
key : kim, hashcode : 321 index : 1
key : park, hashcode : 430 index : 0
key : ko, hashcode : 218 index : 3
key : lee, hashcode : 310 index : 0
my name is kim
my name is park
my name is ko
my name is lee lee
null

Process finished with exit code 0