• java学习_5_24


    TreeMap底层是用红黑树实现的,源码如下:

    /**
     * A Red-Black tree based {@link NavigableMap} implementation.
     * The map is sorted according to the {@linkplain Comparable natural
     * ordering} of its keys, or by a {@link Comparator} provided at map
     * creation time, depending on which constructor is used.
     *
     * <p>This implementation provides guaranteed log(n) time cost for the
     * {@code containsKey}, {@code get}, {@code put} and {@code remove}
     * operations.  Algorithms are adaptations of those in Cormen, Leiserson, and
     * Rivest's <em>Introduction to Algorithms</em>.
    */
    
    static final class Entry<K,V> implements Map.Entry<K,V> {
            K key;
            V value;
            Entry<K,V> left;
            Entry<K,V> right;
            Entry<K,V> parent;
            boolean color = BLACK;

    Set为一接口(没有顺序 不可重复),其实现类HashSet底层实现实为HashTable  JDK源码如下:

    public class HashSet<E>
        extends AbstractSet<E>
        implements Set<E>, Cloneable, java.io.Serializable
    {
        static final long serialVersionUID = -5024744406713321676L;
    
        private transient HashMap<E,Object> map;
    
        // Dummy value to associate with an Object in the backing Map
        private static final Object PRESENT = new Object();
    
        /**
         * Constructs a new, empty set; the backing {@code HashMap} instance has
         * default initial capacity (16) and load factor (0.75).
         */
        public HashSet() {
            map = new HashMap<>();
        }
    
    .............
      public boolean add(E e) {
            return map.put(e, PRESENT)==null;
        }
    
    ........
    }
  • 相关阅读:
    javascritp学习笔记(四)----引用类型
    Bootstrap学习笔记(二)-----网格系统
    Javascript学习笔记(三)--变量、作用域和内存问题
    Javascript学习笔记(二)--创建对象(七种模式)
    Javascript学习笔记(一)--理解对象
    ReentrantLock
    synchronized使用
    多线程在web项目中的存在方式
    多线程基础
    java集合Map体系
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/10920686.html
Copyright © 2020-2023  润新知