Class SetUtils
- java.lang.Object
-
- io.microsphere.collection.SetUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanisSet(java.lang.Class<?> type)Checks whether the specifiedtypeis assignable fromSetinterface.static booleanisSet(java.lang.Object values)Checks whether the specifiedIterableis an instance ofSet.static <E> java.util.Set<E>newFixedHashSet(int size)Creates a new, emptyHashSetwith the specified initial capacity and fixed load factor.static <E> java.util.Set<E>newFixedLinkedHashSet(int size)Creates a new, emptyLinkedHashSetwith the specified initial capacity and fixed load factor.static <E> java.util.Set<E>newHashSet()Creates a new, emptyHashSetwith the default initial capacity and load factor.static <E> java.util.Set<E>newHashSet(int initialCapacity)Creates a new, emptyHashSetwith the specified initial capacity and default load factor.static <E> java.util.Set<E>newHashSet(int initialCapacity, float loadFactor)Creates a new, emptyHashSetwith the specified initial capacity and load factor.static <E> java.util.Set<E>newHashSet(E... elements)Creates a newHashSetcontaining all elements from the provided varargs array.static <E> java.util.Set<E>newHashSet(java.lang.Iterable<E> elements)Creates a newHashSetcontaining all elements from the providedIterable.static <E> java.util.Set<E>newHashSet(java.util.Collection<E> elements)Creates a newHashSetcontaining all elements from the providedCollection.static <E> java.util.Set<E>newLinkedHashSet()Creates a new, emptyLinkedHashSetwith the default initial capacity and load factor.static <E> java.util.Set<E>newLinkedHashSet(int initialCapacity)Creates a new, emptyLinkedHashSetwith the specified initial capacity and default load factor.static <E> java.util.Set<E>newLinkedHashSet(int initialCapacity, float loadFactor)Creates a new, emptyLinkedHashSetwith the specified initial capacity and load factor.static <E> java.util.Set<E>newLinkedHashSet(E... elements)Creates a newLinkedHashSetcontaining all elements from the provided varargs array.static <E> java.util.Set<E>newLinkedHashSet(java.lang.Iterable<E> elements)Creates a newLinkedHashSetcontaining all elements from the providedIterable.static <E> java.util.Set<E>newLinkedHashSet(java.util.Collection<E> elements)Creates a newLinkedHashSetcontaining all elements from the providedCollection.static <E> java.util.Set<E>newLinkedHashSet(java.util.Iterator<E> elements)Creates a newLinkedHashSetcontaining all elements from the providedIterator.static <E> java.util.Set<E>of(E... elements)Creates an unmodifiableSetfrom the given varargs array of elements.static <E> java.util.Set<E>ofSet(E... elements)Creates an unmodifiableSetfrom the given varargs array of elements.static <E> java.util.Set<E>ofSet(java.lang.Iterable<E> elements)Creates an unmodifiableSetfrom the givenIterable.static <T> java.util.Set<T>ofSet(java.util.Collection<T> elements)Creates an unmodifiableSetfrom the givenCollection.static <T> java.util.Set<T>ofSet(java.util.Collection<T> elements, T... others)Creates an unmodifiableSetfrom the givenCollectionand additional varargs elements.static <E> java.util.Set<E>ofSet(java.util.Enumeration<E> elements)Creates an unmodifiableSetfrom the givenEnumeration.
-
-
-
Method Detail
-
isSet
public static boolean isSet(@Nullable java.lang.Object values)
Checks whether the specifiedIterableis an instance ofSet.This method returns
trueif 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:
values- the values to check, may be null- Returns:
trueif the given iterable is aSet; otherwise,false
-
isSet
public static boolean isSet(@Nullable java.lang.Class<?> type)
Checks whether the specifiedtypeis assignable fromSetinterface.This method returns
trueif the provided type is aSetinterface or its sub-interface, or the implementation class ofSetinterface.Example Usage
boolean result1 = SetUtils.isSet(Set.class); // returns true boolean result2 = SetUtils.isSet(HashSet.class); // returns true boolean result3 = SetUtils.isSet(List.class); // returns false boolean result4 = SetUtils.isSet(String.class); // returns false- Parameters:
type- thetypeto check, may be null- Returns:
trueif the given type is aSetinterface or its sub-interface, or the implementation class ofSetinterface.
-
of
@Nonnull public static <E> java.util.Set<E> of(E... elements)
Creates an unmodifiableSetfrom 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
Setcontaining all unique elements from the provided array
-
ofSet
@Nonnull @Immutable public static <E> java.util.Set<E> ofSet(E... elements)
Creates an unmodifiableSetfrom 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
Setcontaining all unique elements from the provided array
-
ofSet
@Nonnull @Immutable public static <E> java.util.Set<E> ofSet(java.util.Enumeration<E> elements)
Creates an unmodifiableSetfrom 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
Setcontaining all unique elements from the provided enumeration
-
ofSet
@Nonnull @Immutable public static <E> java.util.Set<E> ofSet(java.lang.Iterable<E> elements)
Creates an unmodifiableSetfrom 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
Setcontaining all unique elements from the provided iterable
-
ofSet
@Nonnull @Immutable public static <T> java.util.Set<T> ofSet(java.util.Collection<T> elements)
Creates an unmodifiableSetfrom 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
Setcontaining all unique elements from the provided collection
-
ofSet
@Nonnull @Immutable public static <T> java.util.Set<T> ofSet(java.util.Collection<T> elements, T... others)
Creates an unmodifiableSetfrom the givenCollectionand 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
Setcontaining 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 newHashSetcontaining 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
HashSetcontaining all unique elements from the provided iterable
-
newHashSet
@Nonnull public static <E> java.util.Set<E> newHashSet(java.util.Collection<E> elements)
Creates a newHashSetcontaining all elements from the providedCollection.This method delegates to the
HashSetconstructor 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
HashSetcontaining all elements from the provided collection
-
newHashSet
@Nonnull public static <E> java.util.Set<E> newHashSet(E... elements)
Creates a newHashSetcontaining 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
HashSetcontaining all unique elements from the provided array
-
newHashSet
@Nonnull public static <E> java.util.Set<E> newHashSet()
Creates a new, emptyHashSetwith the default initial capacity and load factor.This method provides a convenient way to instantiate an empty
HashSetinstance. 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, emptyHashSetwith the specified initial capacity and default load factor.This method provides a convenient way to instantiate an empty
HashSetinstance 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
HashSetwith the specified initial capacity
-
newHashSet
public static <E> java.util.Set<E> newHashSet(int initialCapacity, float loadFactor)Creates a new, emptyHashSetwith the specified initial capacity and load factor.This method provides a convenient way to instantiate an empty
HashSetinstance 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
HashSetwith the specified initial capacity and load factor
-
newLinkedHashSet
public static <E> java.util.Set<E> newLinkedHashSet(java.lang.Iterable<E> elements)
Creates a newLinkedHashSetcontaining 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
LinkedHashSetcontaining all unique elements from the provided iterable
-
newLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newLinkedHashSet(java.util.Iterator<E> elements)
Creates a newLinkedHashSetcontaining 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
LinkedHashSetcontaining all unique elements from the provided iterator
-
newLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newLinkedHashSet(java.util.Collection<E> elements)
Creates a newLinkedHashSetcontaining all elements from the providedCollection.This method delegates to the
LinkedHashSetconstructor 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
LinkedHashSetcontaining all elements from the provided collection
-
newLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newLinkedHashSet(E... elements)
Creates a newLinkedHashSetcontaining 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
LinkedHashSetcontaining all unique elements from the provided array
-
newLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newLinkedHashSet()
Creates a new, emptyLinkedHashSetwith the default initial capacity and load factor.This method provides a convenient way to instantiate an empty
LinkedHashSetinstance. 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, emptyLinkedHashSetwith the specified initial capacity and default load factor.This method provides a convenient way to instantiate an empty
LinkedHashSetinstance 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
LinkedHashSetwith the specified initial capacity
-
newLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newLinkedHashSet(int initialCapacity, float loadFactor)
Creates a new, emptyLinkedHashSetwith the specified initial capacity and load factor.This method provides a convenient way to instantiate an empty
LinkedHashSetinstance 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
LinkedHashSetwith the specified initial capacity and load factor
-
newFixedHashSet
@Nonnull public static <E> java.util.Set<E> newFixedHashSet(int size)
Creates a new, emptyHashSetwith the specified initial capacity and fixed load factor.This method provides a convenient way to instantiate an empty
HashSetinstance with the given initial capacity and a fixed load factor defined byMapUtils.FIXED_LOAD_FACTOR. The returned set is not thread-safe and allows null elements.Example Usage
Set<String> set = SetUtils.newFixedHashSet(32); // returns a new empty hash set with initial capacity of 32 and fixed load factor- Type Parameters:
E- the type of elements in the set- Parameters:
size- the initial capacity of the returned set- Returns:
- a new empty
HashSetwith the specified initial capacity and fixed load factor - See Also:
MapUtils.FIXED_LOAD_FACTOR
-
newFixedLinkedHashSet
@Nonnull public static <E> java.util.Set<E> newFixedLinkedHashSet(int size)
Creates a new, emptyLinkedHashSetwith the specified initial capacity and fixed load factor.This method provides a convenient way to instantiate an empty
LinkedHashSetinstance with the given initial capacity and a fixed load factor defined byMapUtils.FIXED_LOAD_FACTOR. The returned set is not thread-safe and allows null elements.Example Usage
Set<String> set = SetUtils.newFixedLinkedHashSet(32); // returns a new empty linked hash set with initial capacity of 32 and fixed load factor- Type Parameters:
E- the type of elements in the set- Parameters:
size- the initial capacity of the returned set- Returns:
- a new empty
LinkedHashSetwith the specified initial capacity and fixed load factor - See Also:
MapUtils.FIXED_LOAD_FACTOR
-
-