ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

手动实现HashMap集合

2021-05-05 15:01:28  阅读:208  来源: 互联网

标签:hash HashMap 手动 value key firstNode 集合 currentNode table


/*
* 实现HashMap集合
* put(key,value)
* 1)key-> hash 散列码
* 2)hash& table.length-1 ->index
* 3)if(table[index]==null{
* 直接放
* }else{
* 找key是否存在,如果存在,新值覆盖旧值
* 如果不存在,将key,value封装为一个结点Entry直接添加
*
* }
* */
class MyHashMap<K,V>{
    private  Entry<K,V>[] table;//桶 用来放节点
    private int size;//记录当前节点个数
    private  static final int defaultCapacity=8;

    @Override
    public String toString() {
        return "MyHashMap{" +
                "table=" + Arrays.toString(table) +
                '}';
    }

    class Entry<K,V>{
        private K key;
        private V value;
        protected Entry<K,V> next;
        protected int hash;

        public Entry(K key, V value, int hash) {
            this.key = key;
            this.value = value;
            this.hash = hash;
        }

        @Override
        public String toString() {
            return "Entry{" +
                    "key=" + key +
                    ", value=" + value +
                    ", hash=" + hash +
                    '}';
        }
    }
    public MyHashMap(){
        this(defaultCapacity);
    }
    public MyHashMap(int capacity){
        table=new Entry[capacity];
    }
    public int hash(K key){
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
    public void put(K key,V value){
        int hash=hash(key);//计算散列值
        int index=hash & table.length-1;//获取数组下标
        if(table[index]==null){
            //将key/value封装为一个Entry节点直接放在该位置
            table[index]=new Entry<>(key, value, hash);
            size++;
        }else{
          Entry<K,V> firstNode= table[index];
          //查找是否有重复key
            if(firstNode.key.equals(key)){
                //该key存在于该位置的第一个节点//值覆盖
                firstNode.value=value;
            }else{
                //遍历链表
                 Entry<K,V> currentNode=firstNode;
               while(currentNode.next!=null&&!currentNode.key.equals(key)){
                    currentNode=currentNode.next;
                }
                if(currentNode.next==null){
                    //仅剩最后一个元素没有判断
                    if(currentNode.key.equals(key)){
                        currentNode.value=value;
                    }else{
                        currentNode.next= new Entry<>(key,value,hash);
                        size++;
                    }

                }else{
                 //  currentNode.next!=null&&currentNode.key.equals(key)
                   currentNode.value =value;
                }

            }


        }

    }
    public V get(K key){
        //确定散列码
        int hash=hash(key);
        //确定索引位置
        int index=hash&table.length-1;
        Entry<K,V> firstNode=table[index];
        if(firstNode==null){
            return null;
        }else{
            //遍历列表
            Entry<K,V> currentNode=firstNode;
            while(currentNode!=null){
                if (currentNode.key.equals(key)){
                    return currentNode.value;
                }
                currentNode=currentNode.next;
            }
        }

      return null;

    }
    //移除key所在的entry节点
    public boolean remove(K key){
        int hash=hash(key);
        int index=hash &table.length-1;
        Entry<K,V> firstNode=table[index];
        if(firstNode==null){
            //说明该位置没有元素
            return false;
        }else{
            if(firstNode.key.equals(key)){
                table[index]=firstNode.next;//赋值
                return true;
            }
            //firstNode作为当前节点的前一个
            while(firstNode.next!=null){//只要用到的引用,都要判断它不为空
               if(firstNode.next.key.equals(key)){
                   firstNode.next=firstNode.next.next;
                   return true;
               }else{
                   firstNode=firstNode.next;
               }
            }

            }
            return false;
        }

    }


public class HashMapTest {
    public static void main(String[] args) {
        MyHashMap<String, Integer>  map = new MyHashMap<>();
        map.put("dandan",23);
        map.put("xuanxuan",56);
        map.put("mengchen",12);
        System.out.println(map);
        System.out.println(map.get("dandan"));
        System.out.println(map.remove("dandan"));
        System.out.println(map);

    }
}

标签:hash,HashMap,手动,value,key,firstNode,集合,currentNode,table
来源: https://blog.csdn.net/weixin_49263647/article/details/115801543

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有