手写HashMap源码

Map.java

public interface Map<K,V> {

    public V put(K k,V v);


    public V get(K k);


    public int size();

    interface Entry<K,V>{
        K getKey();

        V getValue();

        V setValue(V v);
    }
}

HashMap.java 

public class HashMap<K,V> implements Map<K,V>{

    Node<K,V>[] array = null;

    private static int defaultLength = 16;

    private static double factor = 0.75D;

    private int size;

    @Override
    public V put(K k, V v) {

        if(array==null){
            array = new Node[defaultLength];
        }

        int index = position(k,defaultLength);

        if(size>defaultLength*factor){
            resize();
        }
        Node<K,V> node = array[index];
        if(node == null){
            array[index] = new Node<K,V>(k,v,null);
            size++;
        }
        return null;
    }

    private void resize(){
        Node<K,V>[] temp = new Node[defaultLength << 1];
        Node<K,V> node = null;

        for(int i = 0;i<array.length;i++){
            node = array[i];
            while (node!=null){
                int index = position(node.getKey(),temp.length);
                Node<K,V> next = node.next;
                node.next=temp[index];
                temp[index] = node;
                node = next;
            }
        }
        array = temp;
        defaultLength = temp.length;
        temp=null;
    }

    private int position(K k,int length){
        int code = k.hashCode();

        return code%(length-1);
    }

    @Override
    public V get(K k) {
        if(array!=null){
            int index=position(k,defaultLength);
            Node<K,V> node = array[index];
            while (node!=null){
                if(node.getKey()==k){
                    return node.getValue();
                }else {
                    node = node.next;
                }
            }
        }
        return null;
    }

    @Override
    public int size() {
        return size;
    }


    static class Node<K,V> implements Entry<K,V>{

        K key;
        V value;
        Node<K,V> next;

        public Node(K key,V value,Node<K,V> next){
            super();
            this.key = key;
            this.value = value;
            this.next = next;
        }

        @Override
        public K getKey() {
            return this.key;
        }

        @Override
        public V getValue() {
            return this.value;
        }

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

测试Test.java

public class Test {
    
    public static void main(String[] args){

        Map<String,String> map = new HashMap<>();
        map.put("u","3");
        System.out.println(map.get("u"));
    }
}

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值