Java 自定义map

Node节点类:

public class Node {
    private Object key;
    private Object value;
    private Node next;
    public Node(){
    }
    public Node(Object key,Object value){
        this.key=key;
        this.value=value;
    }
}

myHashMap类:

public class myMap {
    private int size = 1 << 3;//默认大小8
    private int number = 0;//存储节点的个数
    private List arr = new ArrayList(size);

    public myMap() {
        for (int i = 0; i < size; i++) {
            LinkedList list = new LinkedList();//初始化的为空链表头
            arr.add(list);//初始化时就将空节点添加到数组中去
        }
    }

    /**
     * 根据键值对生成节点,并将节点放入哈希表中
     *
     * @param key
     * @param value
     */
    public void put(Object key, Object value) {
        if (containsKey(key) == true)
            replace(key, value);
        else if (number / size == 10)
            rehash();
        number++;
        Node node = new Node(key, value);
        int code = hashcode(key.toString());//得到哈希码
        int index = locate(code);//得到该哈希码在对应哈希数组中的位置
        LinkedList listhead = arr.get(index);
        listhead.add(node);//将节点放进链表中
    }

    private void rehash() {
    }

    /**
     * 根据键得到值
     *
     * @param key
     * @return
     */
    public Object getValue(Object key) {
        //根据key值找到在数组中的对应位置
        int code = hashcode(key.toString());
        int index = locate(code);
        LinkedList list = arr.get(index);
        for (int i = 0; i < list.size(); i++) {
            Node tNode = (Node) list.get(i);
            while (tNode != null)
                if (tNode.getKey().equals(key))
                    return tNode.getValue();
            tNode = tNode.getNext();
        }
        return null;
    }

    /**
     * 移除节点
     *
     * @param key
     * @return
     */
    public boolean remove(Object key) {
        number--;
        int code = hashcode(key.toString());
        int index = locate(code);
        LinkedList list = arr.get(index);
        for (int i = 0; i < list.size(); i++) {
            Node tNode = (Node) list.get(i);
            while (tNode != null) {
                if (tNode.getKey().equals(key)) {
                    list.remove(i);
                    return true;
                }
                tNode = tNode.getNext();
            }
        }
        return false;
    }

    /**
     * 替换key键的value值
     *
     * @param key
     * @param value
     * @return
     */
    public boolean replace(Object key, Object value) {
        int code = hashcode(key.toString());
        int index = locate(code);
        LinkedList list = arr.get(index);
        for (int i = 0; i < list.size(); i++) {
            Node tNode = (Node) list.get(i);
            if (tNode.getKey().equals(key)) {
                tNode.setValue(value);
                return true;
            }
        }
        return false;
    }

    /**
     * 清空方法
     */
    public void clear() {
        for (int i = 0; i < size; i++) {
            LinkedList list = arr.get(i);
            list.clear();
        }
        number = 0;
    }

    /**
     * 哈希表中含key键,返回true
     *
     * @param key
     * @return
     */
    public boolean containsKey(Object key) {
        int code = hashcode(key.toString());
        int index = locate(code);
        LinkedList list = arr.get(index);
        for (int i = 0; i < list.size(); i++) {
            Node tNode = (Node) list.get(i);
            if (tNode.getKey().equals(key)) {
                return true;
            }
            tNode = tNode.getNext();
        }
        return false;
    }

    /**
     * 哈希表中含value值,返回true
     *
     * @param value
     * @return
     */
    public boolean containsValue(Object value) {
        for (int i = 0; i < size; i++) {
            LinkedList list = arr.get(i);
            for (int j = 0; j < list.size(); j++) {
                Node tNode = (Node) list.get(j);
                while (tNode != null) {
                    if (tNode.getValue().equals(value)) {
                        return true;
                    }
                    tNode = tNode.getNext();
                }
            }
        }
        return false;
    }

    /**
     * 计算字符串的哈希码
     *
     * @param str
     * @return
     */
    public int hashcode(String str) {
        int k = 0;
        for (int i = 0; i < str.length(); i++) {//对str所有域进行计算
            k += str.charAt(i);
        }
        return (size - 1) & k;
    }

    /**
     * 得到哈希码对应在数组具体的位置
     * @param k
     * @return
     */
    public int locate(int k){
        return k%size;
    }

    /**
     * 返回存贮节点的个数
     * @return
     */
    public int size(){
        return number;
    }

}

 

你可能感兴趣的:(java基础)