HashMap重写(4) 增加泛型

在这里插入图片描述

package Study.Collection;

/**
 * <p>Description: </p>
 * 增加泛型,封装
 * @author zhangyan
 * @date 2019/1/29 17:33
 */
public class YanHashMap02<K,V> {

    /**
     * 位桶数组。bucket  array
     */
    Node3[] table;

    /**
     * 存放的键值对的个数
     */
    int size;

    /**
     * 定义构造函数
     */
    public YanHashMap02() {
        //长度一般定义成2的整数幂
        table = new Node3[16];
    }

    /**
     * put方法
     *
     * @param key
     * @param value
     */
    public void put(K key, V value) {

        //定义了新的节点对象
        Node3 newNode = new Node3();
        newNode.hash = myHash(key.hashCode(), table.length);
        int hash = newNode.hash;
        newNode.key = key;
        newNode.value = value;
        newNode.next = null;

        /**
         * temp是对应hash值的节点
         */
        Node3 temp = table[hash];

        //正在遍历的最后一个元素
        Node3 iterLast = null;
        boolean keyRepeat = false;

        if (temp == null) {
            //此处数组元素为空,则直接将新节点放进去
            table[hash] = newNode;
            size++;
        } else {
            //此处数组元素不为空,则遍历对应链表,对链表进行操作
            while (temp != null) {

                //判断key如果重复,则覆盖
                if (temp.key.equals(key)) {
                    keyRepeat = true;
                    //只是覆盖value即可,其他的值(hash,key,next)保持不变
                    temp.value = value;
                    //覆盖之后就不用遍历链表后面的节点
                    break;

                } else {
                    //key不重复,则遍历下一个。
                    iterLast = temp;
                    temp = temp.next;

                }
            }

            //没有发生key重复的情况,则添加到链表最后一个节点
            if(!keyRepeat){
                iterLast.next = newNode;
                size++;
            }
        }
    }

    /**
     * get()方法
     * @param key
     * @return
     */
    public V get(K key) {

        //拿到对应的hash值
        int hash = myHash(key.hashCode(), table.length);
        V value = null;

        if (table[hash] != null) {
            Node3 temp = table[hash];

            while (temp != null) {

                //如果相等,则说明找到了键值对,返回相应的value
                if (temp.key.equals(key)) {
                    value = (V)temp.value;
                    break;
                } else {
                    temp = temp.next;
                }
            }

        }

        return value;
    }

    /**
     * 重写toString()
     *
     * @return
     */
    @Override
    public String toString() {
        //{10:aa, 20:bb}
        StringBuilder sb = new StringBuilder("{");

        //遍历table数组
        for (int i = 0; i < table.length; i++) {
            Node3 temp = table[i];

            //遍历链表
            while (temp != null) {
                sb.append(temp.key + "=" + temp.value + ", ");
                temp = temp.next;
            }
        }
        sb.setCharAt(sb.length() - 1, '}');
        return sb.toString();

    }


    public static void main(String[] args) {
        YanHashMap02<Integer, String> m = new YanHashMap02<>();
        m.put(10, "aa");
        m.put(20, "bb");
        m.put(30, "cc");

        System.out.println(m);
        System.out.println(m.get(10));

    }


    /**
     * 返回Hash值
     *
     * @param v
     * @param length
     * @return
     */
    public static int myHash(int v, int length) {
//		System.out.println("hash in myHash:"+(v&(length-1)));		//直接位运算,效率高
//		System.out.println("hash in myHash:"+(v%(length-1)));		//取模运算,效率低
        return v & (length - 1);
    }
}

节点:

public class Node3<K,V> {

    int hash;
    K key;
    V value;
    Node3<K,V> next;
}

增加泛型的思路也很简单,代码中注释也很清楚。
当然HashMap还有其他的方法,比如remove(),clone() 等等,HashMap还有扩容机制。
这里就不一一实现了,和前面的ArrayList,LinkedList基本差不多。
大家可以自己试着实现一下!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值