What is the time complexity of initializing an HashSet using a populated ArrayList?

一曲冷凌霜 提交于 2020-01-24 11:03:23

问题


I read online that HashSet uses HashMap as its underlying data structure.

And HashMap uses ArrayList as its underlying data structure with each item in the list being an object of LinkedList or a tree.

What is the time complexity when you initialize a HashSet this way? Can it be O(1)? If not, why?

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(3);
list.add(2);
list.add(6);
list.add(0);
HashSet<Integer> set = new HashSet<>(list);

回答1:


HashMap does not use an ArrayList as its underlying data structure.

HashMap maintains a hashtable built out of a primitive array, and uses a hybrid strategy of a linked list or a tree for handling collisions. You can see this by examining the source code for java.util.HashMap.

Since, to build a hashtable, you need to invoke the hash function on every entry to determine the hash location, the minimum bound is O(N). The worst case is for pathologically distributed keys (e.g. every key is a collision), which would be substantially worse than O(N). Since the Java 8 implementation degrades to a balanced tree instead of a linked list in the worst case, your worst case runtime would be O(N log N). (In previous versions of Java, which used linear probing, I believe worst case would have been O(N²)).




回答2:


Your List can contain n number of Objects which might be all duplicate or all unique or mixture.

The property of a HashSet collection is that it contains no duplicate objects.

Therefore, the time complexity to traverse the List and add each object to the HashSet would be O(n)



来源:https://stackoverflow.com/questions/44144187/what-is-the-time-complexity-of-initializing-an-hashset-using-a-populated-arrayli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!