仿写HashMap

这个博客详细介绍了HashMap在Java中的实现,包括初始化、put方法、扩容机制、get方法和其它关键操作。通过源码级别的仿写展示了HashMap内部如何使用数组和链表存储元素,并在容量达到一定阈值时自动扩容。此外,还提供了查找、添加和判断键是否存在等常用功能的实现。
摘要由CSDN通过智能技术生成

HashMap

  • 下面是对HashMap的部分方法的仿写
public class HashMapDemo<K,V> {

    // 数组初始长度
    private static int tar = 16;

    // 阈值
    static final float threshold = 0.75f;
    //
    static int size = 0 ;

    // 初始化参数
    public HashMapDemo() {

    }
    //自己设置数组长度
    public HashMapDemo(int x) {
        tar = x;
    }
    private Node<K,V>[] table;

    //put方法
    public V put(K key, V val){
        if(size==0){
            table = new Node[tar];
        }

        int h = hash(key)%tar;
        if(table[h]==null){
            table[h] = new Node<K, V>(key,val,null);
        }else{
            Node<K,V> temp = table[h];
            while(temp.next!=null){
                if(temp.key==key){
                    temp.val = val;
                    break;
                }
                temp = temp.next;
            }
            if(temp.key!=key){
                temp.next = new Node<K, V>(key,val,null);
            }
        }
        size++;
        if(size>=tar*threshold){
            expansion();
        }
        return val;
    }
    //扩容方法
    private void expansion(){
        tar*=2;
        Node[] temp = new Node[tar];
        for(int i =0;i<tar/2;i++){
            if(table[i]!=null){
                Node node = table[i];
                while(node!=null){
                    int h = hash((K) node.key)%tar;
                    if(temp[h]==null){
                        temp[h] = new Node(node.key,node.val,null);
                    }else{
                        Node node1 =  temp[h];
                        while(node1.next!=null){
                            node1 = node1.next;
                        }
                        node1.next = new Node(node.key,node.val,null);
                    }
                    node = node.next;
                }
            }
        }

        table = temp;
    }
    //添加方法
    public V get(K key){
      int h = hash(key)%tar;
      if(table[h]!=null){
            Node temp = table[h];
            while(temp!=null){
                if(temp.key==key){
                    return (V) temp.val;
                }
                temp = temp.next;
            }
      }
        return null;
    }
    // 获hash值
    private int hash(Object key){
        if(key==null)return 0;
        int x = key.hashCode();
        x = x^(x>>>16);

        return x<0? x*=-1 :x;
    }
    //返回个数
    public int size(){
        return size;
    }
    //判断key是否存在
    public boolean containsKey(K key){
        if(size==0)return false;
        int h = hash(key)%tar;
        if(table[h]!=null){
            Node temp = table[h];
            while(temp!=null){
                if(temp.key==key)return true;
                temp = temp.next;
            }
        }
        return false;
    }
    //
    @Override
    public String toString(){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("{");
        for(int i =0;i<tar;i++){
            if (table[i]!=null){
                Node temp = table[i];
                while(temp!=null){
                    stringBuffer.append(temp);
                    stringBuffer.append(",");
                    temp = temp.next;
                }
            }
        }
        stringBuffer.delete(stringBuffer.length()-1,stringBuffer.length());
        stringBuffer.append("}");
        return stringBuffer.toString();
    }
}

public class Node<K,V> {

    K key;
    V val;
    Node<K, V> next;
    Node(K key,V val,Node<K,V> next){
        this.key = key;
        this.val = val;
        this.next = next;
    }
    public final K getKey(){
        return key;
    }
    public final V getVal(){
        return val;
    }
    @Override
    public final String toString(){
        return key+"="+val;
    }
    //判断是否存在
    @Override
    public final boolean equals(Object key){
        if(key == this.key||key==null) {
            return true;
        }
        return false;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值