自定义HashMap,实现存取键值的功能

import java.util.*;

public class MyMap<K,V> extends AbstractMap {
     private static final int SIZE=1024;//数组的容量
    LinkedList<MyEntry<K,V>>[] buckets=new LinkedList[SIZE];//链表数组,这是Map的基本组成结构


    static class MyEntry<K,V>{  //用来建立key-value映射
       private K key;
       private V value;
       public MyEntry(K key,V value){
           this.key=key;
           this.value=value;
       }

        public K getKey() {
            return key;
        }

        public V getValue() {
            return value;
        }

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

        public int hashCode(){
           return (key==null?0:key.hashCode())^
                   (value==null?0:value.hashCode());
        }
        public boolean equals(Object o){
           if((o instanceof MyEntry))
               return false;
           MyEntry mm=(MyEntry)o;
           return (key==null)?mm.getKey()==null:key.equals(mm.getKey())&&
                   (value==null)?mm.getValue()==null:value.equals(mm.getValue());
        }

        public String toString(){ return key+"="+value;}
    }

    public V push(K key,V value){
        V oldValue=null;
        int index=Math.abs(key.hashCode())%SIZE;//计算出key的哈希码然后取模得到下标
        MyEntry<K,V> entry=new MyEntry<>(key,value);
        if(buckets[index]==null){   
            buckets[index]=new LinkedList<MyEntry<K,V>>();//如果数组中为null,创建新链表放入数组
            LinkedList<MyEntry<K,V>> bucket=buckets[index];
            bucket.add(entry);
        }else {
            LinkedList<MyEntry<K,V>> bucket=buckets[index];  //如果不为null,得到数组对应下标的链表
            ListIterator<MyEntry<K,V>> it=bucket.listIterator();//遍历链表
            boolean flag=false;//用来判断链表中是否已经含有key
            while (it.hasNext()) {
                MyEntry<K, V> ientry = it.next();
                if (ientry.getKey().equals(key)) {
                    oldValue = ientry.getValue();
                    it.set(entry);//链表中已经含有key,修改key对应的value
                    flag = true;
                    break;
                }
            }
            if(!flag){
                buckets[index].add(entry);//如果链表中没有这个key,添加新键值对
            }
        }

            return oldValue;
        }


    public V get(Object key){
        int index=Math.abs(key.hashCode())%SIZE;//先计算key的哈希码,取模得到在数组中的下标
        if(buckets[index]==null)
            return  null;
        for(MyEntry<K,V> ipair:buckets[index])//遍历所处index下标处的链表,找到对应的key
            if(ipair.getKey().equals(key))
                return ipair.getValue();
            return null;
    }
    @Override
    public Set<MyEntry<K,V>> entrySet() {  //用来存储MyEntry对象
        Set<MyEntry<K,V>> set=new HashSet<>();
        for(LinkedList<MyEntry<K,V>> bucket:buckets){
            if(bucket==null)
                continue;
            for(MyEntry<K,V> mpair:bucket)
                set.add(mpair);
            }
        return set;
    }

    public static void main(String[] args) {
        MyMap<String,String> map=new MyMap<>();
        map.push("hello","world");
        map.push("干将","莫邪");
        map.push("梁山伯","祝英台");
        map.push("梁山伯","马文才");
        for(MyMap.MyEntry<String,String> e:map.entrySet()){
            System.out.println(e);
        }
    }
}

来试试效果:

干将=莫邪
hello=world
梁山伯=马文才

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值