How come Java provides several different implementations of the Set
type, including HashSet
and TreeSet
and not ArraySet
?
ArraySet(java.util.Collection c) Creates a set that contains all of the elements of the specified Collection in the order returned by the iterator of the Collection . ArraySet(int initialCapacity) Creates an empty set whose size is the specified initial capacity.
The Java platform contains three general-purpose Set implementations: HashSet , TreeSet , and LinkedHashSet . HashSet , which stores its elements in a hash table, is the best-performing implementation; however it makes no guarantees concerning the order of iteration.
There are three general-purpose Set implementations — HashSet , TreeSet , and LinkedHashSet . Which of these three to use is generally straightforward. HashSet is much faster than TreeSet (constant-time versus log-time for most operations) but offers no ordering guarantees.
LinkedHashSet class is quite similar to the HashSet class; it is an ordered version of HashSet. Moreover, it allows us to maintain the insertion order of the elements. It inherits the HashSet class and implements the Set interface.
A set based solely on an array of elements in no particular order would always have O(n) time for a containment check. It wouldn't be terribly useful, IMO. When would you want to use that instead of HashSet
or TreeSet
?
The most useful aspect of an array is that you can get to an element with a particular index extremely quickly. That's not terribly relevant when it comes to sets.
There is CopyOnWriteArraySet which is a set backed by an array.
This is not particularly useful as its performance is not great for large collections.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With