JAVA基础集合类

Collection结构图:

image2020-4-22_15-19-13.png

Map结构图:

image2020-4-22_15-19-31.png

ArrayList与LinkedList的区别:

image2020-4-22_15-18-59.png

HashMap:

image2020-4-22_15-19-52.png

LinkedHashMap:

image2020-4-22_15-23-42.png

本示意图中,LinkedHashMap—共包含五个节点。除去红色双向虚线来看,在这里其实就是一个正宗的HashMap。


HashMap:

V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))  

对key执行remappingFunction,v可能为空,并将lamdba返回值设为新的value


V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)

对只有当key不存在时执行remappingFunction,v可能为空,并将lamdba返回值设为新的value,若返回值为空,则不建立映射


V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

对只有当key存在时执行remappingFunction,v可能为空,并将lamdba返回值设为新的value,若返回值为空,则删除映射


V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)

map.merge(1, 0, (a, b) -> a + 1);

若key不存在,写value为默认值第二个参数0,若key存在,执行remappingFunction,a为value,b为默认值0


TreeMap:

    /**
     * Compares two keys using the correct comparison method for this TreeMap.
     */
    @SuppressWarnings("unchecked")
    final int compare(Object k1, Object k2) {
        return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
            : comparator.compare((K)k1, (K)k2);
    }

当构造函数传入comparator时使用comparator,否则调用对象自己的compareTo方法判断大小,如果没有实现Comparable则抛出ClassCastException异常


public NavigableMap<K,V> headMap(K toKey, boolean inclusive)

获得所有key一个小于toKey的SortedMap,当相等时,inclusive为true则包含该key,否则不包含,默认为false


public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive)

获得所有key一个大于fromKey的SortedMap,当相等时,inclusive为true则包含该key,否则不包含,默认为true


subMap

    public SortedMap<K, V> subMap(K fromKey, K toKey) {
        return this.subMap(fromKey, true, toKey, false);
    }

所以这个方法也就很明显了

public K firstKey() // public Map.Entry<K,V> firstEntry()

返回最小的key(Entry)

public Map.Entry<K,V> pollFirstEntry()

返回并删除第一个

public K lastKey() // public Map.Entry<K,V> lastEntry()

返回key最大的key(Entry)

public Map.Entry<K,V> pollLastEntry()

返回并删除最后一个

public K floorKey(K key) //public Map.Entry<K,V> floorEntry(K key)

返回所有小于等于key中最大的key(Entry)

public K ceilingKey(K key) // public Map.Entry<K,V> ceilingEntry(K key)

返回所有大于等于key中最小的key(Entry)

EnumMap

    public V put(K key, V value) {
        typeCheck(key);

        int index = key.ordinal();
        Object oldValue = vals[index];
        vals[index] = maskNull(value);
        if (oldValue == null)
            size++;
        return unmaskNull(oldValue);
    }

即用枚举类的ordinal代替了hashcode,提高了hash的效率并避免了hash冲突

IdentityHashMap

    //  HashMap:
    public final int hashCode() {
        return Objects.hashCode(key) ^ Objects.hashCode(value);
    }
    //  IdentityHashMap:
    public int hashCode() {
        int result = 0;
        Object[] tab = table;
        for (int i = 0; i < tab.length; i +=2) {
            Object key = tab[i];
            if (key != null) {
                Object k = unmaskNull(key);
                result += System.identityHashCode(k) ^
                          System.identityHashCode(tab[i + 1]);
            }
        }
        return result;
    }

即主要区别为不使用key的hashcode算法,转而使用System.identityHashCode计算hashcode

WeakHashMap

当key没有其他引用后,key可能因为gc被回收

    WeakHashMap<Integer, String> map = new WeakHashMap<>();
    for (int i = 0; i < 100; i++) {
        map.put(new Integer(1), String.valueOf(i));
    }
    System.gc();
    map.forEach((k, v) -> System.out.println(k + "----" + v));

大概率是没有任何打印的,原因:

    private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> {
        V value;
        final int hash;
        Entry<K,V> next;

        /**
         * Creates new entry.
         */
        Entry(Object key, V value,
              ReferenceQueue<Object> queue,
              int hash, Entry<K,V> next) {
            super(key, queue);
            this.value = value;
            this.hash  = hash;
            this.next  = next;
        }
    ...
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容