[英]HashMap & HashSet size() in Java
我試圖找到有關此信息,但失敗了。
HashMap
或HashSet
size()
如何實現? 它是如何工作的? 它是O(1)
還是O(n)
運算?
這需要O(1)
時間。 HashMap
有一個size
實例變量,在添加或刪除條目時會對其進行修改:
這是HashMap
實現:
/**
* 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
調用后備HashMap
size()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.