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

Java中的HashMap和HashSet size()

  •  0
  • leocrimson  · 技术社区  · 8 年前

    我试图找到有关它的信息,但失败了。

    情况如何 size() 在里面 HashMap HashSet 是否实施?它是如何工作的?它是一个 O(1) O(n) 活动

    1 回复  |  直到 8 年前
        1
  •  6
  •   Eran    8 年前

    它需要 O(1) 时间 HashMap 有一个 size 添加或删除条目时修改的实例变量:

    这是 哈希图 实施:

    /**
     * Returns the number of key-value mappings in this map.
     *
     * @return the number of key-value mappings in this map
     */
    public int size() {
        return size;
    }
    

    HashSet 电话 size() 背衬的 哈希图 .