手写实现HashMap的put和get方法

手写实现HashMap的put和get方法


interface MyMap<K,V> {
    V put(K key,V value);

    V get(K key);
}

public class MyHashMap<K,V> implements MyMap<K,V> {
    private static final int INIT_CAPACITY = 16;
    private static final double LOAD_FACTORY = 0.75f;
    private int initcapacity = 0;
    private double loadfactory = 0f;
    private Entry<K,V>[] table = null;

    //默认构造函数
    public MyHashMap(){
        this.loadfactory = LOAD_FACTORY;
        this.initcapacity = INIT_CAPACITY;
        table = new Entry[this.initcapacity];
    }

    //有参构造函数
    public MyHashMap(int initcapacity,double loadfactory){
        this.initcapacity = initcapacity;
        this.loadfactory = loadfactory;
        table = new Entry[this.initcapacity];
    }

    //hash函数
    private int hash(K key){
        int h;
        return key == null ? 0 : Math.abs(h = key.hashCode()) ^ (h>>>16);
    }

    //equals函数
    private boolean equ(Object a,Object b){
        return(a == b);
    }


    @Override
    public V put(K key, V value) {
        int index = hash(key) % initcapacity;

        if(table[index] != null){
            Entry<K,V> e = table[index];
            Entry<K,V> e2 = null;
            while(e != null){
                if(hash(e.key) == hash(key) && equ(e.key,key)){
                    //当hash值相同且调用equals方法(手撕代码需自己实现)时
                    e.val = value;
                    return value;
                }
                e2 = e;
                e = e.next;
            }
            e2.next = new Entry<>(key, value, null ,index);
        }else{
            Entry<K,V> e = new Entry<>(key,value,null,index);
            table[index] = e;
        }
        return value;
    }

    @Override
    public V get(K key) {
        int index = hash(key)%initcapacity;
        Entry<K,V> e = table[index];
        if(e == null){
            return null;
        }

        while (e != null){
            if(e.getKey() == null && key == null || hash(e.getKey()) == hash(key) && e.getKey().equals(key)){
                return e.getValue();
            }
            e = e.next;
        }
        return null;
    }

    public static void main(String[] args){
        MyMap<String,Object> map = new MyHashMap<>();
        map.put("name","zuoshen");
        map.put("age",23);
        map.put("weight",75);
        map.put(null,"zuozhen2");

        System.out.println(map.get("name"));
        System.out.println(map.get("age"));
        System.out.println(map.get("weight"));
        System.out.println(map.get(null));

        map.put("name","zuozhen1");
        System.out.println(map.get("name"));
    }
}

class Entry<K,V>{
    K key;
    V val;
    Entry<K,V> next;
    int index; // 记录下标

    Entry(K key,V val,Entry<K,V> next,int index){
        this.key = key;
        this.val = val;
        this.next =next;
        this.index = index;
    }

    public K getKey() {
        return key;
    }

    public V getValue() {
        return val;
    }

    public Entry<K,V> getNext(){
        return next;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值