Class SetUtils
- java.lang.Object
-
- io.microsphere.collection.SetUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
isSet(java.lang.Iterable<?> elements)
Checks whether the specifiedIterable
is an instance ofSet
.static <E> java.util.Set<E>
newHashSet()
Creates a new, emptyHashSet
with the default initial capacity and load factor.static <E> java.util.Set<E>
newHashSet(int initialCapacity)
Creates a new, emptyHashSet
with the specified initial capacity and default load factor.static <E> java.util.Set<E>
newHashSet(int initialCapacity, float loadFactor)
Creates a new, emptyHashSet
with the specified initial capacity and load factor.static <E> java.util.Set<E>
newHashSet(E... elements)
Creates a newHashSet
containing all elements from the provided varargs array.static <E> java.util.Set<E>
newHashSet(java.lang.Iterable<E> elements)
Creates a newHashSet
containing all elements from the providedIterable
.static <E> java.util.Set<E>
newHashSet(java.util.Collection<E> elements)
Creates a newHashSet
containing all elements from the providedCollection
.static <E> java.util.Set<E>
newLinkedHashSet()
Creates a new, emptyLinkedHashSet
with the default initial capacity and load factor.static <E> java.util.Set<E>
newLinkedHashSet(int initialCapacity)
Creates a new, emptyLinkedHashSet
with the specified initial capacity and default load factor.static <E> java.util.Set<E>
newLinkedHashSet(int initialCapacity, float loadFactor)
Creates a new, emptyLinkedHashSet
with the specified initial capacity and load factor.static <E> java.util.Set<E>
newLinkedHashSet(E... elements)
Creates a newLinkedHashSet
containing all elements from the provided varargs array.static <E> java.util.Set<E>
newLinkedHashSet(java.lang.Iterable<E> elements)
Creates a newLinkedHashSet
containing all elements from the providedIterable
.static <E> java.util.Set<E>
newLinkedHashSet(java.util.Collection<E> elements)
Creates a newLinkedHashSet
containing all elements from the providedCollection
.static <E> java.util.Set<E>
newLinkedHashSet(java.util.Iterator<E> elements)
Creates a newLinkedHashSet
containing all elements from the providedIterator
.static <E> java.util.Set<E>
of(E... elements)
Creates an unmodifiableSet
from the given varargs array of elements.static <E> java.util.Set<E>
ofSet(E... elements)
Creates an unmodifiableSet
from the given varargs array of elements.static <E> java.util.Set<E>
ofSet(java.lang.Iterable<E> elements)
Creates an unmodifiableSet
from the givenIterable
.static <T> java.util.Set<T>
ofSet(java.util.Collection<T> elements)
Creates an unmodifiableSet
from the givenCollection
.static <T> java.util.Set<T>
ofSet(java.util.Collection<T> elements, T... others)
Creates an unmodifiableSet
from the givenCollection
and additional varargs elements.static <E> java.util.Set<E>
ofSet(java.util.Enumeration<E> elements)
Creates an unmodifiableSet
from the givenEnumeration
.
-
-
-
Method Detail
-
isSet
public static boolean isSet(@Nullable java.lang.Iterable<?> elements)
Checks whether the specifiedIterable
is an instance ofSet
.This method returns
true
if the provided iterable is aSet
, ensuring that operations like duplicate elimination and order independence are already handled by the implementation.Example Usage
Set<String> set = new HashSet<>(); set.add("apple"); set.add("banana"); boolean result1 = SetUtils.isSet(set); // returns true List<String> list = Arrays.asList("apple", "banana"); boolean result2 = SetUtils.isSet(list); // returns false
- Parameters:
elements
- theIterable
to check, may be null- Returns:
true
if the given iterable is aSet
; otherwise,false
-
of
@Nonnull public static <E> java.util.Set<E> of(E... elements)
Creates an unmodifiableSet
from the given varargs array of elements.This method converts the provided array into a set to eliminate duplicates, and returns it as an unmodifiable view. If the input array is null or empty, an empty set is returned.
Example Usage
Set<String> set1 = SetUtils.of("apple", "banana", "apple"); // returns an unmodifiable set containing ["apple", "banana"] String[] fruits = {"apple", "banana"}; Set<String> set2 = SetUtils.of(fruits); // returns an unmodifiable set containing ["apple", "banana"] Set<String> emptySet = SetUtils.of(); // returns an empty unmodifiable set
- Type Parameters:
E
- the type of elements in the array- Parameters:
elements
- the array of elements to add to the set, may be null or empty- Returns:
- an unmodifiable
Set
containing all unique elements from the provided array
-
ofSet
@Nonnull public static <E> java.util.Set<E> ofSet(E... elements)
Creates an unmodifiableSet
from the given varargs array of elements.This method converts the provided array into a set to eliminate duplicates, and returns it as an unmodifiable view. If the input array is null or empty, an empty set is returned.
Example Usage
Set<String> set1 = SetUtils.ofSet("apple", "banana", "apple"); // returns an unmodifiable set containing ["apple", "banana"] String[] fruits = {"apple", "banana"}; Set<String> set2 = SetUtils.ofSet(fruits); // returns an unmodifiable set containing ["apple", "banana"] Set<String> emptySet = SetUtils.ofSet(); // returns an empty unmodifiable set
- Type Parameters:
E
- the type of elements in the array- Parameters:
elements
- the array of elements to add to the set, may be null or empty- Returns:
- an unmodifiable
Set
containing all unique elements from the provided array
-
ofSet
@Nonnull public static <E> java.util.Set<E> ofSet(java.util.Enumeration<E> elements)
Creates an unmodifiableSet
from the givenEnumeration
.This method iterates through the provided enumeration and adds each element to a new set, ensuring uniqueness, and returns it as an unmodifiable view. If the enumeration is null or has no elements, an empty set is returned.
Example Usage
Vector<String> vector = new Vector<>(); vector.add("apple"); vector.add("banana"); Enumeration<String> enumeration = vector.elements(); Set<String> set = SetUtils.ofSet(enumeration); // returns an unmodifiable set containing ["apple", "banana"] Set<String> emptySet = SetUtils.ofSet(null); // returns an empty unmodifiable set
- Type Parameters:
E
- the type of elements in the enumeration- Parameters:
elements
- the enumeration of elements to add to the set, may be null or empty- Returns:
- an unmodifiable
Set
containing all unique elements from the provided enumeration
-
ofSet
@Nonnull public static <E> java.util.Set<E> ofSet(java.lang.Iterable<E> elements)
Creates an unmodifiableSet
from the givenIterable
.This method iterates through the provided iterable and adds each element to a new set, ensuring uniqueness, and returns it as an unmodifiable view. If the iterable is null or empty, an empty set is returned.
Example Usage
List<String> list = Arrays.asList("apple", "banana", "apple"); Set<String> set1 = SetUtils.ofSet(list); // returns an unmodifiable set containing ["apple", "banana"] Set<String> emptySet = SetUtils.ofSet(null); // returns an empty unmodifiable set
- Type Parameters:
E
- the type of elements in the iterable- Parameters:
elements
- the iterable of elements to add to the set, may be null or empty- Returns:
- an unmodifiable
Set
containing all unique elements from the provided iterable
-
ofSet
@Nonnull public static <T> java.util.Set<T> ofSet(java.util.Collection<T> elements)
Creates an unmodifiableSet
from the givenCollection
.This method adds all elements from the provided collection to a new set, ensuring uniqueness, and returns it as an unmodifiable view. If the collection is null or empty, an empty set is returned.
Example Usage
List<String> list = Arrays.asList("apple", "banana", "apple"); Set<String> set1 = SetUtils.ofSet(list); // returns an unmodifiable set containing ["apple", "banana"] Set<String> emptySet = SetUtils.ofSet(null); // returns an empty unmodifiable set
- Type Parameters:
T
- the type of elements in the collection- Parameters:
elements
- the collection of elements to add to the set, may be null or empty- Returns:
- an unmodifiable
Set
containing all unique elements from the provided collection
-
ofSet
@Nonnull public static <T> java.util.Set<T> ofSet(java.util.Collection<T> elements, T... others)
Creates an unmodifiableSet
from the givenCollection
and additional varargs elements.This method combines all elements from the provided collection and the varargs array into a new set, ensuring uniqueness, and returns it as an unmodifiable view. If both the collection and varargs array are null or empty, an empty set is returned.
Example Usage
List<String> list = Arrays.asList("apple", "banana"); Set<String> set1 = SetUtils.ofSet(list, "orange", "grape"); // returns an unmodifiable set containing ["apple", "banana", "orange", "grape"] Set<String> set2 = SetUtils.ofSet(null, "apple", "banana"); // returns an unmodifiable set containing ["apple", "banana"] Set<String> emptySet = SetUtils.ofSet(Collections.emptyList(), (String[]) null); // returns an empty unmodifiable set
- Type Parameters:
T
- the type of elements in the collection and varargs- Parameters:
elements
- the collection of elements to add to the set, may be null or emptyothers
- the additional elements to include in the set, may be null or empty- Returns:
- an unmodifiable
Set
containing all unique elements from the provided collection and varargs
-
newHashSet
@Nonnull public static <E> java.util.Set<E> newHashSet(java.lang.Iterable<E> elements)
Creates a newHashSet
containing all elements from the providedIterable
.This method iterates through the given iterable and adds each element to the newly created set, ensuring uniqueness. If the input iterable is null or empty, a new empty set will still be returned.
Example Usage
List<String> list = Arrays.asList("apple", "banana", "apple"); Set<String> set = SetUtils.newHashSet(list); // returns a hash set containing ["apple", "banana"] Set<String> emptySet = SetUtils.newHashSet(null); // returns a new empty hash set
- Type Parameters:
E
- the type of elements in the iterable- Parameters:
elements
- the iterable of elements to add to the set, may be null or empty- Returns:
- a new
HashSet
containing all unique elements from the provided iterable
-
newHashSet
@Nonnull public static <E> java.util.Set<E> newHashSet(java.util.Collection<E> elements)
Creates a newHashSet
containing all elements from the providedCollection
.This method delegates to the
HashSet
constructor that accepts a collection, ensuring all elements from the input collection are included in the resulting set. The returned set is not thread-safe and allows null elements.Example Usage
List<String> list = Arrays.asList("apple", "banana", "apple"); Set<String> set = SetUtils.newHashSet(list); // returns a hash set containing ["apple", "banana"] Set<String> emptySet = SetUtils.newHashSet(Collections.emptyList()); // returns a new empty hash set
- Type Parameters:
E
- the type of elements in the collection- Parameters:
elements
- the collection of elements to add to the set, may be null or empty- Returns:
- a new
HashSet
containing all elements from the provided collection
-
newHashSet
@Nonnull public static <E> java.util.Set<E> newHashSet(E... elements)
Creates a newHashSet
containing all elements from the provided varargs array.This method adds each element from the input array to the newly created set, ensuring uniqueness. The insertion order is not preserved as it uses
HashSet
. If the input array is null or empty, a new empty set will still be returned.Example Usage
Set<String> set1 = SetUtils.newHashSet("apple", "banana", "apple"); // returns a hash set containing ["apple", "banana"] String[] fruits = {"apple", "banana"}; Set<String> set2 = SetUtils.newHashSet(fruits); // returns a hash set containing ["apple", "banana"] Set<String> emptySet = SetUtils.newHashSet(); // returns a new empty hash set
- Type Parameters:
E
- the type of elements in the array- Parameters:
elements
- the array of elements to add to the set, may be null or empty- Returns:
- a new
HashSet
containing all unique elements from the provided array
-
newHashSet
@Nonnull public static <E> java.util.Set<E> newHashSet()
Creates a new, emptyHashSet
with the default initial capacity and load factor.This method provides a convenient way to instantiate an empty
HashSet
instance. The returned set is not thread-safe and allows null elements.Example Usage
Set<String> set = SetUtils.newHashSet(); // returns a new empty hash set with default initial capacity (16) and load factor (0.75)
- Type Parameters:
E
- the type of elements in the set- Returns:
- a new empty
HashSet
-
newHashSet
@Nonnull public static <E> java.util.Set<E> newHashSet(int initialCapacity)
Creates a new, emptyHashSet
with the specified initial capacity and default load factor.This method provides a convenient way to instantiate an empty
HashSet
instance with the given initial capacity. The returned set is not thread-safe and allows null elements.Example Usage
Set<String> set = SetUtils.newHashSet(32); // returns a new empty hash set with initial capacity of 32 and default load factor (0.75)
- Type Parameters:
E
- the type of elements in the set- Parameters:
initialCapacity
- the initial capacity of the returned set- Returns:
- a new empty
HashSet
with the specified initial capacity
-
newHashSet
public static <E> java.util.Set<E> newHashSet(int initialCapacity, float loadFactor)
Creates a new, emptyHashSet
with the specified initial capacity and load factor.This method provides a convenient way to instantiate an empty
HashSet
instance with the given initial capacity and load factor. The returned set is not thread-safe and allows null elements.Example Usage
Set<String> set = SetUtils.newHashSet(32, 0.5f); // returns a new empty hash set with initial capacity of 32 and load factor of 0.5
- Type Parameters:
E
- the type of elements in the set- Parameters:
initialCapacity
- the initial capacity of the returned setloadFactor
- the load factor of the returned set- Returns:
- a new empty
HashSet
with the specified initial capacity and load factor
-
newLinkedHashSet
public static <E> java.util.Set<E> newLinkedHashSet(java.lang.Iterable<E> elements)
Creates a newLinkedHashSet
containing all elements from the providedIterable
.This method iterates through the given iterable and adds each element to the newly created set, preserving insertion order. If the input iterable is null or empty, an empty set will still be returned.
Example Usage
List<String> list = Arrays.asList("apple", "banana", "apple"); Set<String> set = SetUtils.newLinkedHashSet(list); // returns a linked hash set containing ["apple", "banana"] Set<String> emptySet = SetUtils.newLinkedHashSet(null); // returns a new empty linked hash set
- Type Parameters:
E
- the type of elements in the iterable- Parameters:
elements
- the iterable of elements to add to the set, may be null or empty- Returns:
- a new
LinkedHashSet
containing all unique elements from the provided iterable
-
newLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newLinkedHashSet(java.util.Iterator<E> elements)
Creates a newLinkedHashSet
containing all elements from the providedIterator
.This method iterates through the given iterator and adds each element to the newly created set, preserving the insertion order. If the input iterator is null or has no elements, an empty set will still be returned.
- Type Parameters:
E
- the type of elements in the iterator- Parameters:
elements
- the iterator of elements to add to the set, may be null or empty- Returns:
- a new
LinkedHashSet
containing all unique elements from the provided iterator
-
newLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newLinkedHashSet(java.util.Collection<E> elements)
Creates a newLinkedHashSet
containing all elements from the providedCollection
.This method delegates to the
LinkedHashSet
constructor that accepts a collection, ensuring all elements from the input collection are included in the resulting set while preserving insertion order. The returned set is not thread-safe and allows null elements.Example Usage
List<String> list = Arrays.asList("apple", "banana", "apple"); Set<String> set = SetUtils.newLinkedHashSet(list); // returns a linked hash set containing ["apple", "banana"] Set<String> emptySet = SetUtils.newLinkedHashSet(Collections.emptyList()); // returns a new empty linked hash set
- Type Parameters:
E
- the type of elements in the collection- Parameters:
elements
- the collection of elements to add to the set, may be null or empty- Returns:
- a new
LinkedHashSet
containing all elements from the provided collection
-
newLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newLinkedHashSet(E... elements)
Creates a newLinkedHashSet
containing all elements from the provided varargs array.This method adds each element from the input array to the newly created set, ensuring uniqueness while preserving the insertion order. If the input array is null or empty, a new empty set will still be returned.
Example Usage
Set<String> set1 = SetUtils.newLinkedHashSet("apple", "banana", "apple"); // returns a linked hash set containing ["apple", "banana"] String[] fruits = {"apple", "banana"}; Set<String> set2 = SetUtils.newLinkedHashSet(fruits); // returns a linked hash set containing ["apple", "banana"] Set<String> emptySet = SetUtils.newLinkedHashSet(); // returns a new empty linked hash set
- Type Parameters:
E
- the type of elements in the array- Parameters:
elements
- the array of elements to add to the set, may be null or empty- Returns:
- a new
LinkedHashSet
containing all unique elements from the provided array
-
newLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newLinkedHashSet()
Creates a new, emptyLinkedHashSet
with the default initial capacity and load factor.This method provides a convenient way to instantiate an empty
LinkedHashSet
instance. The returned set is not thread-safe and allows null elements.Example Usage
Set<String> set = SetUtils.newLinkedHashSet(); // returns a new empty linked hash set with default initial capacity (16) and load factor (0.75)
- Type Parameters:
E
- the type of elements in the set- Returns:
- a new empty
LinkedHashSet
-
newLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newLinkedHashSet(int initialCapacity)
Creates a new, emptyLinkedHashSet
with the specified initial capacity and default load factor.This method provides a convenient way to instantiate an empty
LinkedHashSet
instance with the given initial capacity. The returned set is not thread-safe and allows null elements.Example Usage
Set<String> set = SetUtils.newLinkedHashSet(32); // returns a new empty linked hash set with initial capacity of 32 and default load factor (0.75)
- Type Parameters:
E
- the type of elements in the set- Parameters:
initialCapacity
- the initial capacity of the returned set- Returns:
- a new empty
LinkedHashSet
with the specified initial capacity
-
newLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newLinkedHashSet(int initialCapacity, float loadFactor)
Creates a new, emptyLinkedHashSet
with the specified initial capacity and load factor.This method provides a convenient way to instantiate an empty
LinkedHashSet
instance with the given initial capacity and load factor. The returned set is not thread-safe and allows null elements.Example Usage
Set<String> set = SetUtils.newLinkedHashSet(32, 0.5f); // returns a new empty linked hash set with initial capacity of 32 and load factor of 0.5
- Type Parameters:
E
- the type of elements in the set- Parameters:
initialCapacity
- the initial capacity of the returned setloadFactor
- the load factor of the returned set- Returns:
- a new empty
LinkedHashSet
with the specified initial capacity and load factor
-
-