代码之家  ›  专栏  ›  技术社区  ›  sap

hashset是通过hashmap实例实现的

  •  1
  • sap  · 技术社区  · 16 年前

    我想知道既然hashset是通过hashmap实例实现的,那么将数据放入hashset的键是什么。

    我通过了链接 http://www.coderanch.com/t/251832/Programmer-Certification-SCJP/certification/Difference-HashMap-HashSet

    我不太明白。 有谁能帮我更好地理解它吗

    4 回复  |  直到 16 年前
        1
  •  6
  •   danben    16 年前

    来源:

    // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();
    
    
    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }
    
        2
  •  5
  •   Charles Ma    16 年前

    因为映射的键是集合,所以键将是进入散列集本身的对象。

        3
  •  1
  •   Lombo    16 年前

    其思想是使用添加到 HashSet 作为 HashMap 是的。这样的话 add 我是说, remove ,和 contains 在O(1)中运行。

        4
  •  0
  •   oksayt    15 年前

    是的( source code here )中。hashset本质上是hashmap键集的接口。

       /**
        * HashSet is an implementation of a Set. All optional operations (adding and
        * removing) are supported. The elements can be any objects.
        */
       public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable,
               Serializable {
    
           private static final long serialVersionUID = -5024744406713321676L;
    
           transient HashMap<E, HashSet<E>> backingMap; // right here!