• 【java基础】java集合之HashTable,HashSet,HashMap


    【一】HashSet

    (1)HashSet内部维护的是一个HashMap,具体原理见java集合之HashMap

    【二】HashTable

    (1)HashTable内部维护的是一个Entry的数组。Entry又是一个单项链表的结构。

    private static class Entry<K,V> implements Map.Entry<K,V> {
            final int hash;
            final K key;
            V value;
            Entry<K,V> next;
    
            protected Entry(int hash, K key, V value, Entry<K,V> next) {
                this.hash = hash;
                this.key =  key;
                this.value = value;
                this.next = next;
            }
    
            @SuppressWarnings("unchecked")
            protected Object clone() {
                return new Entry<>(hash, key, value,
                                      (next==null ? null : (Entry<K,V>) next.clone()));
            }
    
            // Map.Entry Ops
    
            public K getKey() {
                return key;
            }
    
            public V getValue() {
                return value;
            }
    
            public V setValue(V value) {
                if (value == null)
                    throw new NullPointerException();
    
                V oldValue = this.value;
                this.value = value;
                return oldValue;
            }
    
            public boolean equals(Object o) {
                if (!(o instanceof Map.Entry))
                    return false;
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
    
                return (key==null ? e.getKey()==null : key.equals(e.getKey())) &&
                   (value==null ? e.getValue()==null : value.equals(e.getValue()));
            }
    
            public int hashCode() {
                return hash ^ Objects.hashCode(value);
            }
    
            public String toString() {
                return key.toString()+"="+value.toString();
            }
        }
    View Code

    (2)

  • 相关阅读:
    2019年10月31日 万能异常
    2019年10月29日 异常处理
    2019年10月26日 复习
    爬虫时如何使用代理服务器
    爬虫时url中http和https的区别
    博客园如何自定义博客皮肤和主题
    Python发送QQ邮件
    Python中的XML
    持久化-pickle和shelve
    open()函数提示找不到file的解决办法
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/8157607.html
Copyright © 2020-2023  润新知