package com.apps;

import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JavaHash {

    private List<ArrayList<SimpleEntry<String, Object>>> data;
    private int size = 16;

    public JavaHash() {

        data = new ArrayList<ArrayList<SimpleEntry<String, Object>>>(size);

        for (int a = 0; a < size; a++) {
            data.add(new ArrayList<SimpleEntry<String, Object>>());
        }

    }

    public void put(String key, Object value) {

        List<SimpleEntry<String, Object>> list = data.get(getHashIndex(key));

        if (list == null)
            list = new ArrayList<SimpleEntry<String, Object>>();

        if (list.size() > 0) {
            for (SimpleEntry<String, Object> entry : list) {
                if (entry.getKey().equals(key)) {
                    entry.setValue(value);
                }
            }
        } else {
            SimpleEntry<String, Object> entry = new SimpleEntry<String, Object>(key, value);
            list.add(entry);
        }

    }

    private int getHashIndex(String key) {

        return key.hashCode() % size;

    }

    public SimpleEntry<String, Object> get(String key) {

        List<SimpleEntry<String, Object>> list = data.get(getHashIndex(key));

        for (SimpleEntry<String, Object> entry : list) {
            if (entry.getKey().equals(key)) {
                return entry;
            }
        }

        return null;
    }
    
    public Boolean remove(String key) {
        List<SimpleEntry<String, Object>> list = data.get(getHashIndex(key));

        for (SimpleEntry<String, Object> entry : list) {
            if (entry.getKey().equals(key)) {
                list.remove(entry);
                return true;
            }
        }        
        
        return false;
    }

    public static void main(String args[]) {

        JavaHash hash = new JavaHash();
        Map<String, String> map = new HashMap<String,String> ();

        hash.put("bird", "eagle");
        hash.put("bird", "maya");
        hash.put("dog", "doberman");
        hash.put("dog", "shih-tzu");
        hash.put("dog", "rottweiler");

        System.out.println(hash.get("bird"));
        System.out.println(hash.get("dog"));
        
        hash.remove("bird");
        hash.remove("dog");
        
        System.out.println(hash.get("bird"));
        
        // List<String> list = new ArrayList<String>(10);
        // System.out.println(list.size());

    }

}