自定义MyHashMap集合及entrySet()实现

 即通过写一个MyHashMap<K,V>来彻底理解HashMap<K,V>以及其的EntrySet()方法,理解此Map后自然会理解HashSet<>,具体性能不敢妄自比较JDK,其他方法实现或许因过于简单或者与Put()、Get()方法大同小异未在其中体现;已在主方法调用过此MyHashSet<K,V>,无bug。纯手打;


import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 自定义MyHAshMap
 *
 * @param <K> 泛值,键的类型
 * @param <V> 泛值,值的类型
 */
public class MyHashMap<K, V> {
    //    private. LinkedList<Entry<K,V>>[] table =new LinkedList[99];
    private LinkedList<Entry<K, V>>[] tables = new LinkedList[100];//使用LinkedList<>[]实现HashMap<>

    /**
     * 此put()实现了HashMap中的put()
     * @param key 键
     * @param value 值
     */
    public void put(K key, V value) {
        //第一步 先将key,value封装成Entry对象
        Entry<K, V> entry = new Entry<>(key, value);

        //第二步:把key的哈希值转换成数组中的索引
        int index = Math.abs(key.hashCode() % 99);//int

        if (tables[index] == null) {
            //创建链表,并且放到对应的索引处
            tables[index] = new LinkedList<>();
            tables[index].add(entry);//把数据放到链表里
        } else {//这一列已经有数据了,就需要拿当前的key值和这一列中所有的对象的key进行比较
            boolean flag = false;
            for (Entry<K, V> e : tables[index]) {
                if (e.getKey().equals(key)) {
                    flag = true;//键相同,直接修改此键的Value
                    e.setValue(value);
                }
            }
            // 如果flag=false说明这一列没有相同的数据,存入
            if (flag == false) {
                tables[index].add(entry);
            }
        }
    }
    /**
     * 此方法为根据键找对应的值 get方法二代目
     *
     * @param key 键
     * @return 键相应的值或null
     */
    public V get(K key) {
        //第一步计算索引
        int index = Math.abs(key.hashCode() % 99);//int
        //第二步:拿一列所有数据和key进行比较,如果一致就返回对应的值
        if (tables[index] == null) {
            return null;
        } else {
            for (Entry<K, V> entry : tables[index]) {
                if (entry.getKey().equals(key)) {
                    return entry.getValue();
                }
            }
        }
        return null;
    }

    /**
     * 此方法用于获取当前HashMap中所有已存入了entry的数组索引,记录在一个集合中
     * 并返回这个集合
     *
     * @return 一个存储了数组中所有不为空的索引
     */
    private ArrayList<Integer> getIndex() {
        ArrayList<Integer> x = new ArrayList<>();
        for (int i = 0; i < tables.length; i++) {
            if (tables[i] != null) {
                x.add(i);
            }
        }
        return x;
    }

    LinkedList<Entry<K, V>> ent = new LinkedList<>();//此集合用于存放当前HashMap中的entry,初始为空

    /**
     * 此集合用于获取某个已知数组索引中的所有entry,并存放在ent中
     *
     * @param i 指定的数组索引值
     * @return 该索引下的所有entry
     */
    private LinkedList<Entry<K, V>> getoneKeyAndValue(int i) {
        for (int j = 0; j < tables[i].size(); j++) {
            ent.add(tables[i].get(j));
        }
        return ent;
    }

    /**
     * 此方法等同于HashMap中的entrySet(),获取MyHashMap中的所有键值对(entry),
     * 可在主方法中直接用增强for调用
     *
     * @return
     */
    public LinkedList<Entry<K, V>> EntrySet() {
        LinkedList<Entry<K, V>> entries = new LinkedList<>();//创建一个LinkedList用于存放最后我们要的所有entry
        ArrayList<Integer> b = getIndex();//调用getIndex方法,存入b中
        for (Integer index : b) {//对b遍历
            entries = getoneKeyAndValue(index);//调用getoneKeyAndValue方法,将ent地址传给entris
        }
        return entries;//最后返回的entris即现MyHashSet中所有Entry
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值