使用LinkedList实现1.7版本HashMap

public interface YouthMap<K,V> {

    /**
     * 集合的大小
     * @return
     */
    int size();

    /**
     * 添加元素
     * @param key
     * @param val
     * @return
     */
    V put(K key,V val);

    /**
     * 根据key查询
     * @param key
     * @return
     */
    V get(K key);

    interface  Entry<K,V>{
        K getKey();
        V getValue();
        V setValue(V value);
    }
}

/**
 * @title:LinkedListMap
 * @description:使用LinkedList实现1.7版本HashMap
 * @author:laizy
 * @date:2020/8/323:31
 * hashMap为什么能够查询效率非常高(jdk1.7版本) 直接通过计算hash值 使用hash定位直接数组存放位置 查询效率非常高的
 */
public class LinkedListMap<K,V> implements YouthMap<K,V> {

    //存放我们的hashMap元素
    private Object[] objects = new Object[100];
    private LinkedList<Entry>[] entrys=new LinkedList[100];

    @Override
    public int size() {
        return entrys.length;
    }

    @Override
    public V put(K key, V val) {
        int hash = hash(key);
        LinkedList<Entry> linkedList = entrys[hash];
        Entry entry = new Entry(key, val);
        if (linkedList == null) {
            // 2. 没有发生hashCode冲突问题
            linkedList = new LinkedList();
            linkedList.add(entry);
            entrys[hash] = linkedList;
            return val;
        }
        // hashCode发生了碰撞问题 检查是否是同一个key
        for (Entry ety : linkedList) {
            // 如果 hashCode相同且equals也相同说明 相同的key  直接覆盖
            if (ety.getKey().equals(key)) {
                ety.setValue(val);
                return val;
            }
        }
        //hashCode相同但是对象值不同
        linkedList.add(entry);
        entrys[hash] = linkedList;
        return val;
    }

    private int hash(K key) {
        int hashCode = key.hashCode();
        int hash = hashCode % objects.length;
        return hash;
    }

    @Override
    public V get(K key) {
        int hash = hash(key);
        LinkedList<Entry> linkedList = entrys[hash];
        for (Entry ety : linkedList) {
            // 如果 hashCode相同且equals也相同说明 相同的key  直接覆盖
            if (ety.getKey().equals(key)) {
                return (V) ety.getValue();
            }
        }
        return null;
    }

    class Entry<K, V> implements YouthMap.Entry<K, V> {
        // key
        private K k;
        // value
        private V v;

        public K getKey() {
            return this.k;
        }

        public V getValue() {
            return this.v;
        }

        public V setValue(V value) {
            this.v = v;
            return this.v;
        }

        public Entry(K k, V v) {
            this.k = k;
            this.v = v;
        }
    }
}
public class Test {
    public static void main(String[] args) {
        LinkedListMap<String, String> myMap = new LinkedListMap<String, String>();
        myMap.put("key", "value");
        System.out.println(myMap.get("key"));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值