Package com.google.common.collect
Class ImmutableSortedSet.Builder<E>
java.lang.Object
com.google.common.collect.ImmutableCollection.Builder<E>
com.google.common.collect.ImmutableSet.Builder<E>
com.google.common.collect.ImmutableSortedSet.Builder<E>
- Enclosing class:
ImmutableSortedSet<E>
A builder for creating immutable sorted set instances, especially
public static final
sets ("constant sets"), with a given comparator. Example:
public static final ImmutableSortedSet<Number> LUCKY_NUMBERS =
new ImmutableSortedSet.Builder<Number>(ODDS_FIRST_COMPARATOR)
.addAll(SINGLE_DIGIT_PRIMES)
.add(42)
.build();
Builder instances can be reused; it is safe to call build()
multiple times to build
multiple sets in series. Each set is a superset of the set created before it.
- Since:
- 2.0
-
Field Summary
FieldsFields inherited from class com.google.common.collect.ImmutableSet.Builder
forceCopy
Fields inherited from class com.google.common.collect.ImmutableCollection.Builder
DEFAULT_INITIAL_CAPACITY
-
Constructor Summary
ConstructorsConstructorDescriptionBuilder
(Comparator<? super E> comparator) Creates a new builder.Builder
(Comparator<? super E> comparator, int expectedSize) Creates a new builder with an expected size. -
Method Summary
Modifier and TypeMethodDescriptionAddselement
to theImmutableSortedSet
.Adds each element ofelements
to theImmutableSortedSet
, ignoring duplicate elements (only the first duplicate element is added).Adds each element ofelements
to theImmutableSortedSet
, ignoring duplicate elements (only the first duplicate element is added).Adds each element ofelements
to theImmutableSortedSet
, ignoring duplicate elements (only the first duplicate element is added).build()
Returns a newly-createdImmutableSortedSet
based on the contents of theBuilder
and its comparator.(package private) ImmutableSortedSet.Builder
<E> combine
(ImmutableSet.Builder<E> builder) (package private) void
copy()
private void
Methods inherited from class com.google.common.collect.ImmutableSet.Builder
copyIfNecessary, forceJdk
Methods inherited from class com.google.common.collect.ImmutableCollection.Builder
expandedCapacity
-
Field Details
-
comparator
-
elements
-
n
private int n
-
-
Constructor Details
-
Builder
Creates a new builder. The returned builder is equivalent to the builder generated byImmutableSortedSet.orderedBy(java.util.Comparator<E>)
. -
Builder
Builder(Comparator<? super E> comparator, int expectedSize) Creates a new builder with an expected size.
-
-
Method Details
-
copy
void copy()- Overrides:
copy
in classImmutableSet.Builder<E>
-
sortAndDedup
private void sortAndDedup() -
add
Addselement
to theImmutableSortedSet
. If theImmutableSortedSet
already containselement
, thenadd
has no effect. (only the previously added element is retained).- Overrides:
add
in classImmutableSet.Builder<E>
- Parameters:
element
- the element to add- Returns:
- this
Builder
object - Throws:
NullPointerException
- ifelement
is null
-
add
Adds each element ofelements
to theImmutableSortedSet
, ignoring duplicate elements (only the first duplicate element is added).- Overrides:
add
in classImmutableSet.Builder<E>
- Parameters:
elements
- the elements to add- Returns:
- this
Builder
object - Throws:
NullPointerException
- ifelements
contains a null element
-
addAll
Adds each element ofelements
to theImmutableSortedSet
, ignoring duplicate elements (only the first duplicate element is added).- Overrides:
addAll
in classImmutableSet.Builder<E>
- Parameters:
elements
- the elements to add to theImmutableSortedSet
- Returns:
- this
Builder
object - Throws:
NullPointerException
- ifelements
contains a null element
-
addAll
Adds each element ofelements
to theImmutableSortedSet
, ignoring duplicate elements (only the first duplicate element is added).- Overrides:
addAll
in classImmutableSet.Builder<E>
- Parameters:
elements
- the elements to add to theImmutableSortedSet
- Returns:
- this
Builder
object - Throws:
NullPointerException
- ifelements
contains a null element
-
combine
- Overrides:
combine
in classImmutableSet.Builder<E>
-
build
Returns a newly-createdImmutableSortedSet
based on the contents of theBuilder
and its comparator.- Overrides:
build
in classImmutableSet.Builder<E>
-