Home [Data Structure] HashSet vs TreeSet
Post
Cancel

[Data Structure] HashSet vs TreeSet

📌 Common feature

  • No duplicate elements
  • Only one null element

📌 Comparison

  • Most important difference between HashSet and TreeSet is ordering.
HashSetTreeSet
HashSet doesn’t guaranteed any orderTreeSet maintains objects in Sorted order defined by either Comparable or Comparator method in Java.
HashSet is faster than a TreeSet. 

📌 Time complexity

||HashSet|TreeSet| |:—:|:—:|:—:| |Add|O(1)|O(log n)| |Contains|O(1)|O(log n)| |Next|O(h/n)|O(log n)|

Tips O(1) < O(log n) < O(n) < O(n * log n) < O(n²) < O(n³) < O(2^n) < O(n!)


The source : https://soft.plusblog.co.kr/74

This post is licensed under CC BY 4.0 by the author.

[Algorithm] Big-O notation

[Binary Search] Search in Rotated Sorted Array

Comments powered by Disqus.