1.1. HashSet

  • HashSet是线程不安全
  • 底层使用HashMap,hastSet的add值使用的HashMap的key做存储,如下源代码:
    /**
       * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
       * default initial capacity (16) and load factor (0.75).
       */
      public HashSet() {
          map = new HashMap<>();
      }
    
    上面代码是初始化HashSet的时候底层使用HashMap初始化长度16,0.75的加载因子
    public boolean add(E e) {
          return map.put(e, PRESENT)==null;
      }
    
Copyright & copy lviter@163.com            updated 2024-02-06 09:54:55

results matching ""

    No results matching ""