• HashMap简易版


    转载https://www.cnblogs.com/vitalq/p/9615659.html

    public interface BRX_MAP<K,V> {
    
        V put(K key, V value);
    
        V get(K key);
    
        Integer size();
    
        interface Entry<K,V> {
            K getKey();
            V getVaule();
        }
    }
    public class BHashMap<K,V> implements BRX_MAP<K,V> {
    
        public static void main(String[] args) {
            BHashMap<String,String> hashMap = new BHashMap<>();
            hashMap.put("name","brx");
            String name = hashMap.get("name");
            System.out.println(name);
            hashMap.put("name","hqs");
            name = hashMap.get("name");
            System.out.println(name);
        }
    
        //存储数据的数组 ,每一个Entry是一个链表
        private Entry<K, V>[] table;
    
        private Integer size = 0;
    
        //默认大小
        private Integer defaultLength = 16;
    
        //负载因子
        private double loadFactor = 0.75D;
    
        BHashMap() {
            this.table = new Entry[defaultLength];
        }
    
        Integer getIndex(K key) {
            int index = Math.abs((key.hashCode() % (defaultLength - 1)));
            return index;
        }
    
        @Override
        public V put(K key, V value) {
            Integer index = getIndex(key);
            System.out.println("INDEX:"+index);
            //扩容
            if (table.length <= index + 1) {
                Integer len = table.length + (int) (table.length * loadFactor);
                Entry<K, V>[] newTable = Arrays.copyOf(table, len);
                this.table = newTable;
            }
    
            //判断下标是否被占用
            Entry<K, V> kvEntry = table[index];
            //没有被占用
            if (kvEntry == null) {
                table[index] = new Entry(key, value, null, index);
                size++;
            } else {
                //判断是否相同的key
                if (kvEntry.key.equals(key)) {
                    //覆盖相同的key对应的value值
                    table[index] = new Entry(key, value, kvEntry.next, index);
                } else {
                    //把新的值放进去   新进元素,从链表的头部插入(头插法)
                    table[index] = new Entry(key, value, kvEntry, index);
                    size++;
                }
            }
            return table[index].getVaule();
    
        }
    
        @Override
        public V get(K key) {
            Integer index = getIndex(key); 
            Entry<K, V> kvEntry = table[index];//先从数组里找到对应的entry,再从entry里循环遍历链表,equals(key)
            do {
                if (kvEntry == null) {
                    return null;
                }
                if (kvEntry.key.equals(key)) {
                    return table[index].getVaule();
                }
                kvEntry = kvEntry.next;
            } while (true);
    
        }
    
    
        @Override
        public Integer size() {
            return size;
        }
    
        class Entry<K, V> implements BRX_MAP.Entry<K, V> {
    
            private Entry<K, V> next;
            private Integer index;
            private K key;
            private V value;
    
            Entry(K _key, V _value, Entry<K, V> _next, Integer _index) {
                this.key = _key;
                this.value = _value;
                this.next = _next;
                this.index = _index;
            }
    
            Entry() {
            }
    
            @Override
            public K getKey() {
                return this.key;
            }
    
            @Override
            public V getVaule() {
                return this.value;
            }
    
    
        }
    }
  • 相关阅读:
    unicode 编码表(转)
    浅谈中日韩越统一表意文字
    计算机语言发展史
    赋值内存potential leak of an object stored into
    控件方法MFC对话框编程(二):控件的访问
    软件道老子的软件之道 道篇 37 无为而治
    分析脚本文件AndroidInitProcess分析心得(1)
    nullnullWord2010中,空格显示为省略号(点)的解决办法!
    程序环境基于 IO密集 & CPU密集考量 SAN & NAS 选择的一点建议
    总结原因认识自己做事习惯的方法
  • 原文地址:https://www.cnblogs.com/brxHqs/p/11691685.html
Copyright © 2020-2023  润新知