zoukankan      html  css  js  c++  java
  • 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;
        }
    
    ........
    }
  • 相关阅读:
    08-12 NOIP模拟测试18
    08-09 NOIP模拟测试15
    08-11 NOIP模拟测试17
    08-10 NOIP模拟测试16
    08-07 NOIP模拟测试14
    08-03 NOIP模拟测试12
    [SDOI2011]拦截导弹
    08-01 NOIP模拟测试11
    零散知识点
    07-29 NOIP模拟测试10
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/10920686.html
Copyright © 2011-2022 走看看