Class Sets<E>
- Type Parameters:
E- The element type.
Set instances with various configuration options.
This builder provides a flexible and type-safe way to construct sets with support for adding elements, collections, arrays, sorting, and applying modifiers like unmodifiable or sparse modes. Sets automatically handle duplicates - adding the same element multiple times will result in only one occurrence in the final set.
Features:
- Fluent API - all methods return
this for method chaining - Multiple add methods - single elements, varargs, collections, arrays
- Arbitrary input support - automatic type conversion with
addAny(Object...) - Conditional adding -
addIf(boolean, Object)for conditional elements - Sorting support - natural order or custom
Comparator - Sparse mode - return
null for empty sets - Unmodifiable mode - create immutable sets
- Filtering support - exclude unwanted elements via
filtered()orfiltered(Predicate) - Custom conversion functions - type conversion via
elementFunction(Function) - Automatic deduplication - duplicate elements are automatically removed
Examples:
Thread Safety:
This class is not thread-safe. Each builder instance should be used by a single thread or properly synchronized.
See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a single value to this set.Adds multiple values to this set.addAll(Collection<E> value) Appends the contents of the specified collection into this set.Adds arbitrary values to this set.Adds a value to this set if the specified flag is true.Adds entries to this set via JSON array strings.build()Builds the set.Builds the set as aFilteredSet.Builds the set and wraps it in aFluentSet.When specified,build()will return a thread-safe synchronized set.concurrent(boolean value) Sets whetherbuild()should return a thread-safe synchronized set.static <E> Sets<E>Static creator.elementFunction(Function<Object, E> elementFunction) Sets the element conversion function for converting elements inaddAny(Object...).elementType(Class<E> value) Specifies the element type on this list.filtered()Applies a default filter that excludes common "empty" or "unset" values from being added to the set.Applies a filter predicate to elements when building the set.ordered()When specified,build()will use aLinkedHashSetto preserve insertion order.ordered(boolean value) Sets whetherbuild()should use aLinkedHashSetto preserve insertion order.sorted()Converts the set into aSortedSet.sorted(Comparator<E> comparator) Converts the set into aSortedSetusing the specified comparator.sparse()When specified, thebuild()method will returnnull if the set is empty.When specified,build()will return an unmodifiable set.
-
Constructor Details
-
Sets
Constructor.- Parameters:
elementType- The element type. Must not benull .
-
-
Method Details
-
create
Static creator.- Type Parameters:
E- The element type.- Parameters:
elementType- The element type. Must not benull .- Returns:
- A new builder.
-
add
Adds a single value to this set.Note: Filtering is applied at build time, not when adding elements.
- Parameters:
value- The value to add to this set.- Returns:
- This object.
-
add
Adds multiple values to this set.- Parameters:
values- The values to add to this set.- Returns:
- This object.
-
addAll
Appends the contents of the specified collection into this set.This is a no-op if the value is
null .- Parameters:
value- The collection to add to this set.- Returns:
- This object.
-
addAny
Adds arbitrary values to this set.Objects can be any of the following:
- The same type or convertible to the element type of this set.
- Collections or arrays of anything on this set.
- JSON array strings parsed and convertible to the element type of this set.
- Parameters:
values- The values to add.- Returns:
- This object.
-
addIf
Adds a value to this set if the specified flag is true.- Parameters:
flag- The flag.value- The value.- Returns:
- This object.
-
addJson
Adds entries to this set via JSON array strings.- Parameters:
values- The JSON array strings to parse and add to this set.- Returns:
- This object.
-
build
Builds the set.Applies filtering, sorting, ordering, concurrent, unmodifiable, and sparse options.
Set type selection:
- If
sorted()is set: UsesTreeSet(or synchronized TreeSet if concurrent) - If
ordered()is set: UsesLinkedHashSet(or synchronized LinkedHashSet if concurrent) - Otherwise: Uses
HashSet(or synchronized HashSet if concurrent)
If filtering is applied, the result is wrapped in a
FilteredSet.- Returns:
- The built set, or
nullifsparse()is set and the set is empty.
- If
-
buildFluent
-
buildFiltered
Builds the set as aFilteredSet.Set type selection:
- If
sorted()is set: UsesTreeSet(or synchronized TreeSet if concurrent) - If
ordered()is set: UsesLinkedHashSet(or synchronized LinkedHashSet if concurrent) - Otherwise: Uses
HashSet(or synchronized HashSet if concurrent)
Example:
import static org.apache.juneau.commons.utils.CollectionUtils.*; FilteredSet<Integer>set = Sets.create (Integer.class ) .filtered(v -> v !=null && v > 0) .add(5) .add(-1)// Will be filtered out .buildFiltered();Note: If
unmodifiable()is set, the returned set will be wrapped in an unmodifiable view, which may cause issues if the FilteredSet tries to modify it internally. It's recommended to avoid usingunmodifiable()when calling this method.- Returns:
- The built set as a
FilteredSet, ornullifsparse()is set and the set is empty.
- If
-
elementFunction
Sets the element conversion function for converting elements inaddAny(Object...).The function is applied to each element when adding elements in
addAny(Object...).- Parameters:
elementFunction- The function to convert elements. Must not benull .- Returns:
- This object.
-
elementType
Specifies the element type on this list.- Parameters:
value- The element type. Must not benull .- Returns:
- This object.
-
filtered
Applies a default filter that excludes common "empty" or "unset" values from being added to the set.The following values are filtered out:
nullBoolean.FALSE- Numbers with
intValue() == -1 - Empty arrays
- Empty
Maps - Empty
Collections
Example:
import static org.apache.juneau.commons.utils.CollectionUtils.*; Set<Object>set = Sets.create (Object.class ) .filtered() .add("name" ) .add(-1)// Filtered out at build time .add(false )// Filtered out at build time .add(new String[0])// Filtered out at build time .build();- Returns:
- This object.
-
filtered
Applies a filter predicate to elements when building the set.The filter receives the element value. Elements where the predicate returns
truewill be kept; elements where it returnsfalsewill be filtered out.This method can be called multiple times. When called multiple times, all filters are combined using AND logic - an element must pass all filters to be kept in the set.
Note: Filtering is applied at build time, not when adding elements.
Example:
import static org.apache.juneau.commons.utils.CollectionUtils.*;// Keep only non-null, positive integers Set<Integer>set = Sets.create (Integer.class ) .filtered(v -> v !=null && v > 0) .add(5) .add(-1)// Filtered out at build time .add(null )// Filtered out at build time .build();// Multiple filters combined with AND Set<Integer>set2 = Sets.create (Integer.class ) .filtered(v -> v !=null )// First filter .filtered(v -> v > 0)// Second filter (ANDed with first) .filtered(v -> v < 100);// Third filter (ANDed with previous) .add(5) .add(150)// Filtered out (not < 100) .add(-1)// Filtered out (not > 0) .build();- Parameters:
filter- The filter predicate. Must not benull .- Returns:
- This object.
-
sorted
Converts the set into aSortedSet.Note: If
ordered()was previously called, calling this method will override it. The last method called (ordered()orsorted()) determines the final set type.- Returns:
- This object.
-
sorted
Converts the set into aSortedSetusing the specified comparator.Note: If
ordered()was previously called, calling this method will override it. The last method called (ordered()orsorted()) determines the final set type.- Parameters:
comparator- The comparator to use for sorting. Must not benull .- Returns:
- This object.
-
sparse
When specified, thebuild()method will returnnull if the set is empty.Otherwise
build()will never returnnull .- Returns:
- This object.
-
unmodifiable
When specified,build()will return an unmodifiable set.- Returns:
- This object.
-
concurrent
When specified,build()will return a thread-safe synchronized set.The thread-safety implementation depends on other settings:
- If
sorted()is set: Uses synchronizedTreeSet - If
ordered()is set: Uses synchronizedLinkedHashSet - Otherwise: Uses synchronized
HashSet
This is useful when the set needs to be accessed from multiple threads.
Example:
// Create a thread-safe set using synchronized HashSet Set<String>set = Sets.create (String.class ) .add("one" ,"two" ) .concurrent() .build();// Create a thread-safe ordered set Set<String>set2 = Sets.create (String.class ) .ordered() .concurrent() .add("one" ) .build();- Returns:
- This object.
- If
-
concurrent
Sets whetherbuild()should return a thread-safe synchronized set.The thread-safety implementation depends on other settings:
- If
sorted()is set: Uses synchronizedTreeSet - If
ordered()is set: Uses synchronizedLinkedHashSet - Otherwise: Uses synchronized
HashSet
This is useful when the set needs to be accessed from multiple threads.
Example:
// Conditionally create a thread-safe set Set<String>set = Sets.create (String.class ) .add("one" ,"two" ) .concurrent(needsThreadSafety ) .build();- Parameters:
value- Whether to make the set thread-safe.- Returns:
- This object.
- If
-
ordered
When specified,build()will use aLinkedHashSetto preserve insertion order.If not specified, a
HashSetis used by default (no guaranteed order).Note: If
sorted()was previously called, calling this method will override it. The last method called (ordered()orsorted()) determines the final set type.Example:
// Create an ordered set (preserves insertion order) Set<String>set = Sets.create (String.class ) .ordered() .add("one" ) .add("two" ) .build();- Returns:
- This object.
-
ordered
Sets whetherbuild()should use aLinkedHashSetto preserve insertion order.If
false (default), aHashSetis used (no guaranteed order). Iftrue , aLinkedHashSetis used (preserves insertion order).Note: If
sorted()was previously called, calling this method withtrue will override it. The last method called (ordered()orsorted()) determines the final set type.Example:
// Conditionally create an ordered set Set<String>set = Sets.create (String.class ) .ordered(preserveOrder ) .add("one" ) .build();- Parameters:
value- Whether to preserve insertion order.- Returns:
- This object.
-