Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Java implementations for arrayset

Tags:

java

set

How come Java provides several different implementations of the Set type, including HashSet and TreeSet and not ArraySet?

like image 609
user1595291 Avatar asked Aug 13 '12 12:08

user1595291


People also ask

What is 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.

What implements Set Java?

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.

Which Set is faster in Java?

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.

Which implementation of Set is ordered?

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.


2 Answers

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.

like image 157
Jon Skeet Avatar answered Sep 25 '22 23:09

Jon Skeet


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.

like image 39
Peter Lawrey Avatar answered Sep 23 '22 23:09

Peter Lawrey