自己手写实现HashMap

接口设计

/**
 * 定义键值对接口
 */
public interface Map {

    /**
     * 获取Map实体的个数
     * @return
     */
    int size();

    /**
     * 判断是否为空
     * @return
     */
    boolean isEmpty();


    /**
     * 获取 key对应的值
     * @param key
     * @return
     */
    String get(String key);


    /**
     * 添加一对
     * @param key
     * @param value
     * @return
     */
    String put(String key,String value) throws Exception;


    /**
     * 更新已经存在的值
     * @param key
     * @param value
     * @return
     */
    String update(String key,String value);


    /**
     * 按照key删除
     * @param key
     * @return
     */
    String remove(String key);

    /**
     * 删除所有元素
     */
    void clear();


    /**
     * 判断是否相等
     * @param object
     * @return
     */
    boolean equals(Object object);


    interface Entity{
        String getValue();
        String getKey();
        void setValue(String value);
        void setKey(String key);
    }
}

 接口实现类


import java.util.Arrays;

public class HashMap implements Map{


    private int base;

    private final float change;

    private Node[] table;

    private int size;

    private final int deep;
    private int current;


    private final int multiple;

    public HashMap(){
        this(16,0.75f,6,2);
    }

    public HashMap(int base,float change,int deep,int multiple){
        this.base = base;
        this.table = new Node[base];
        this.change = change;
        this.size = 0;
        this.deep = deep;
        this.current = 0;
        this.multiple = multiple;
    }

    private void resize(){
        if (current > deep || (size + 1) > deep*base*change){
            Node[] arrays = new Node[(base+ 1)*multiple];
            this.base = (base+ 1)*multiple;
            current = 0;
            for (Node node : table) {
                Node nodes = node;
                if (nodes != null) {
                    while (nodes != null) {
                        String key = nodes.getKey();
                        int hashcode = hashcode(key);

                        if (arrays[hashcode] == null) {
                            arrays[hashcode] = new Node(nodes.getKey(), nodes.getValue());
                        } else {
                            int deeper = 0;
                            Node nod = arrays[hashcode];
                            while (nod.next != null) {
                                nod = nod.next;
                                deeper++;
                            }
                            current = Math.max(deeper, current);
                            nod.next = new Node(nodes.getKey(), nodes.getValue());
                        }
                        nodes = nodes.next;
                    }
                }
            }
            this.table = arrays;
        }
    }

    private int hashcode(String ops){
        return Math.abs(ops.hashCode()%base);
    }



    static class Node implements Map.Entity{
        private String key;
        private String value;
        private Node next;

        public Node(){

        }

        public Node(String key,String value){
            this.key = key;
            this.value = value;
        }
        /********* setter and getter *********/
        public String getValue() {
            return value;
        }
        public String getKey() {
            return key;
        }
        public void setValue(String value) {
            this.value = value;
        }
        public void setKey(String key) {
            this.key = key;
        }
        public boolean equals(Node node){
            return node.getKey().equals(key) && node.getValue().equals(value);
        }
        @Override
        public String toString() {
            return "Node{" +
                    "key='" + key + '\'' +
                    ", value='" + value + '\'' +
                    '}';
        }
    }


    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public String get(String key) {
        int code = hashcode(key);
        Node nodes = table[code];
        if (nodes != null){
            while (nodes != null && !nodes.getKey().equals(key)){
                nodes = nodes.next;
            }
            return nodes==null?"null":nodes.getValue();
        }else {
            return null;
        }
    }

    public String put(String key, String value) {
        resize();
        int code = hashcode(key);
        if (table[code] == null){
            table[code] = new Node(key,value);
        }else {
            int deeper = 0;
            Node nodes = table[code];
            while (nodes.next != null){
                if (nodes.getKey().equals(key)){
                    return null;
                }
                nodes = nodes.next;
                deeper++;
            }
            current = Math.max(deeper,current);
            nodes.next = new Node(key,value);
        }
        size++;
        return value;
    }

    public String update(String key, String value) {
        int code = hashcode(key);
        Node nodes = table[code];
        if (nodes != null){
            while (nodes != null && !nodes.getKey().equals(key)){
                nodes = nodes.next;
            }
            if (nodes != null){
                nodes.setValue(value);
                return nodes.getValue();
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    public String remove(String key) {
        int code = hashcode(key);
        Node nodes = table[code];
        if (nodes != null){
            while (!nodes.next.getKey().equals(key)){
                nodes = nodes.next;
            }
            String tmp = nodes.next.getValue();
            nodes.next = nodes.next.next;
            return tmp;
        }
        return null;
    }

    public void clear() {
        Arrays.fill(table, null);
    }

    public boolean equals(Map map) {
        if (map.size() != size){
            return false;
        }else {
            for (Node node : table) {
                Node nodes = node;
                if (nodes != null) {
                    while (nodes != null) {
                        String key = nodes.getKey();
                        String value = nodes.getValue();
                        String s = map.get(key);
                        if (!value.equals(s)) {
                            return false;
                        }
                        nodes = nodes.next;
                    }
                }
            }
            return true;
        }
    }

    public void print(){
        for (Node node : table) {
            Node nodes = node;
            if (nodes != null) {
                while (nodes != null) {
                    String key = nodes.getKey();
                    String value = nodes.getValue();
                    System.out.print(key + "=" + value + "\t");
                    nodes = nodes.next;
                }
                System.out.println();
            }
        }
    }

    @Override
    public String toString() {
        StringBuilder buffer = new StringBuilder();
        buffer.append("{");
        for (Node node : table) {
            Node nodes = node;
            if (nodes != null) {
                while (nodes != null) {
                    String key = nodes.getKey();
                    String value = nodes.getValue();
                    buffer.append("'").
                            append(key).append("'").
                            append(": ").append("'").
                            append(value).append("'").append(",");

                    nodes = nodes.next;
                }
            }
        }
        if (buffer.charAt(buffer.length()-1) == ','){
            return buffer.substring(0,buffer.length()-1)+"}";
        }
        return "{}";
    }
}

!!!这里没有设计泛型的实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值