Creates a new HashSet with initialCapacity hash buckets and the
given loadFactor, which must be a positive number. When the ratio of
the number of elements in the set to the number of hash buckets is greater
than or equal to the load factor, the number of buckets is increased.
publicHashSet(int initialCapacity)
Creates a new HashSet with initialCapacity and a default load
factor.
publicHashSet()
Creates a new HashSet with a default initial capacity and load factor.
publicHashSet(Collection<? extends E> coll)
Creates a new HashSet whose initial contents are the elements in coll.
The initial capacity is based on the size of coll, and the default load factor
is used.
The initial capacity and load factor are used to construct the HashMap that underlies a HashSet. The
meaning of these values is discussed in "HashMap" on page 590. Iteration of a HashSet requires a time
proportional to the sum of the size and capacity.
21.5.2. LinkedHashSet
LinkedHashSet
the elements in the set. Iterating through the contents of a LinkedHashset will return the elements in
insertion orderthe order in which they were added. Aside from this, LinkedHashSet behaves the same as
HashSet and defines constructors with the same form. The performance of a LinkedHashSet is likely to
be a little slower than a HashSet because of the overhead of maintaining the linked list structure; however,
iteration only requires a time propertional to the size, regardless of capacity.
21.5.3. treeSet
If you need a SortedSet, you can use treeSet, which stores its contents in a tree structure that is kept
balanced. This means that the time required to modify or search the tree is. treeSet has four constructors:
publictreeSet()
Creates a new treeSet that is sorted according to the natural order of the
element types. All elements added to this set must implement the
Comparable interface and be mutually comparable.
publictreeSet(Collection<? extends E> coll)
Equivalent to using TReeSet() and then adding the elements of coll.
publictreeSet(Comparator<? super E> comp)
Creates a new treeSet that is sorted according to the order imposed by
comp.