Skip to content

io microsphere collection SetUtils

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

SetUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.collection | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/collection/SetUtils.java

Overview

The utilities class for Java Set

Declaration

public abstract class SetUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

isSet

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

isSet

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

of

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

ofSet

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

ofSet

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

ofSet

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

ofSet

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

ofSet

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

newHashSet

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

newHashSet

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

newHashSet

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

newHashSet

Set<String> set = SetUtils.newHashSet();
// returns a new empty hash set with default initial capacity (16) and load factor (0.75)

newHashSet

Set<String> set = SetUtils.newHashSet(32);
// returns a new empty hash set with initial capacity of 32 and default load factor (0.75)

newHashSet

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

newLinkedHashSet

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

newLinkedHashSet

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

newLinkedHashSet

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

newLinkedHashSet

Set<String> set = SetUtils.newLinkedHashSet();
// returns a new empty linked hash set with default initial capacity (16) and load factor (0.75)

newLinkedHashSet

Set<String> set = SetUtils.newLinkedHashSet(32);
// returns a new empty linked hash set with initial capacity of 32 and default load factor (0.75)

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.collection.SetUtils;

API Reference

Public Methods

Method Description
isSet Checks whether the specified Iterable is an instance of Set.
isSet Checks whether the specified Class type is assignable from Set interface.
of Creates an unmodifiable Set from the given varargs array of elements.
ofSet Creates an unmodifiable Set from the given varargs array of elements.
ofSet Creates an unmodifiable Set from the given Enumeration.
ofSet Creates an unmodifiable Set from the given Iterable.
ofSet Creates an unmodifiable Set from the given Collection.
ofSet Creates an unmodifiable Set from the given Collection and additional varargs elements.
newHashSet Creates a new HashSet containing all elements from the provided Iterable.
newHashSet Creates a new HashSet containing all elements from the provided Collection.
newHashSet Creates a new HashSet containing all elements from the provided varargs array.
newHashSet Creates a new, empty HashSet with the default initial capacity and load factor.
newHashSet Creates a new, empty HashSet with the specified initial capacity and default load factor.
newHashSet Creates a new, empty HashSet with the specified initial capacity and load factor.
newLinkedHashSet Creates a new LinkedHashSet containing all elements from the provided Iterable.
newLinkedHashSet Creates a new LinkedHashSet containing all elements from the provided Iterator.
newLinkedHashSet Creates a new LinkedHashSet containing all elements from the provided Collection.
newLinkedHashSet Creates a new LinkedHashSet containing all elements from the provided varargs array.
newLinkedHashSet Creates a new, empty LinkedHashSet with the default initial capacity and load factor.
newLinkedHashSet Creates a new, empty LinkedHashSet with the specified initial capacity and default load factor.

Method Details

isSet

public static boolean isSet(@Nullable Object values)

Checks whether the specified Iterable is an instance of Set.

This method returns true if the provided iterable is a Set, ensuring that operations like duplicate elimination and order independence are already handled by the implementation.

Example Usage

`Set set = new HashSet<>();
set.add("apple");
set.add("banana");

boolean result1 = SetUtils.isSet(set); // returns true

List list = Arrays.asList("apple", "banana");
boolean result2 = SetUtils.isSet(list); // returns false
`

isSet

public static boolean isSet(@Nullable Class<?> type)

Checks whether the specified Class type is assignable from Set interface.

This method returns true if the provided type is a Set interface or its sub-interface, or the implementation class of Set interface.

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
`

of

public static <E> Set<E> of(E... elements)

Creates an unmodifiable Set 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 set1 = SetUtils.of("apple", "banana", "apple");
// returns an unmodifiable set containing ["apple", "banana"]

String[] fruits = {"apple", "banana"`;
Set set2 = SetUtils.of(fruits);
// returns an unmodifiable set containing ["apple", "banana"]

Set emptySet = SetUtils.of();
// returns an empty unmodifiable set
}

ofSet

public static <E> Set<E> ofSet(E... elements)

Creates an unmodifiable Set 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 set1 = SetUtils.ofSet("apple", "banana", "apple");
// returns an unmodifiable set containing ["apple", "banana"]

String[] fruits = {"apple", "banana"`;
Set set2 = SetUtils.ofSet(fruits);
// returns an unmodifiable set containing ["apple", "banana"]

Set emptySet = SetUtils.ofSet();
// returns an empty unmodifiable set
}

ofSet

public static <E> Set<E> ofSet(Enumeration<E> elements)

Creates an unmodifiable Set from the given Enumeration.

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 vector = new Vector<>();
vector.add("apple");
vector.add("banana");
Enumeration enumeration = vector.elements();

Set set = SetUtils.ofSet(enumeration);
// returns an unmodifiable set containing ["apple", "banana"]

Set emptySet = SetUtils.ofSet(null);
// returns an empty unmodifiable set
`

ofSet

public static <E> Set<E> ofSet(Iterable<E> elements)

Creates an unmodifiable Set from the given Iterable.

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 list = Arrays.asList("apple", "banana", "apple");
Set set1 = SetUtils.ofSet(list);
// returns an unmodifiable set containing ["apple", "banana"]

Set emptySet = SetUtils.ofSet(null);
// returns an empty unmodifiable set
`

ofSet

public static <T> Set<T> ofSet(Collection<T> elements)

Creates an unmodifiable Set from the given Collection.

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 list = Arrays.asList("apple", "banana", "apple");
Set set1 = SetUtils.ofSet(list);
// returns an unmodifiable set containing ["apple", "banana"]

Set emptySet = SetUtils.ofSet(null);
// returns an empty unmodifiable set
`

ofSet

public static <T> Set<T> ofSet(Collection<T> elements, T... others)

Creates an unmodifiable Set from the given Collection 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 list = Arrays.asList("apple", "banana");
Set set1 = SetUtils.ofSet(list, "orange", "grape");
// returns an unmodifiable set containing ["apple", "banana", "orange", "grape"]

Set set2 = SetUtils.ofSet(null, "apple", "banana");
// returns an unmodifiable set containing ["apple", "banana"]

Set emptySet = SetUtils.ofSet(Collections.emptyList(), (String[]) null);
// returns an empty unmodifiable set
`

newHashSet

public static <E> Set<E> newHashSet(Iterable<E> elements)

Creates a new HashSet containing all elements from the provided Iterable.

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 list = Arrays.asList("apple", "banana", "apple");
Set set = SetUtils.newHashSet(list);
// returns a hash set containing ["apple", "banana"]

Set emptySet = SetUtils.newHashSet(null);
// returns a new empty hash set
`

newHashSet

public static <E> Set<E> newHashSet(Collection<E> elements)

Creates a new HashSet containing all elements from the provided Collection.

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 list = Arrays.asList("apple", "banana", "apple");
Set set = SetUtils.newHashSet(list);
// returns a hash set containing ["apple", "banana"]

Set emptySet = SetUtils.newHashSet(Collections.emptyList());
// returns a new empty hash set
`

newHashSet

public static <E> Set<E> newHashSet(E... elements)

Creates a new HashSet 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 set1 = SetUtils.newHashSet("apple", "banana", "apple");
// returns a hash set containing ["apple", "banana"]

String[] fruits = {"apple", "banana"`;
Set set2 = SetUtils.newHashSet(fruits);
// returns a hash set containing ["apple", "banana"]

Set emptySet = SetUtils.newHashSet();
// returns a new empty hash set
}

newHashSet

public static <E> Set<E> newHashSet()

Creates a new, empty HashSet 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 set = SetUtils.newHashSet();
// returns a new empty hash set with default initial capacity (16) and load factor (0.75)
`

newHashSet

public static <E> Set<E> newHashSet(int initialCapacity)

Creates a new, empty HashSet 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 set = SetUtils.newHashSet(32);
// returns a new empty hash set with initial capacity of 32 and default load factor (0.75)
`

newHashSet

public static <E> Set<E> newHashSet(int initialCapacity, float loadFactor)

Creates a new, empty HashSet 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 set = SetUtils.newHashSet(32, 0.5f);
// returns a new empty hash set with initial capacity of 32 and load factor of 0.5
`

newLinkedHashSet

public static <E> Set<E> newLinkedHashSet(Iterable<E> elements)

Creates a new LinkedHashSet containing all elements from the provided Iterable.

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 list = Arrays.asList("apple", "banana", "apple");
Set set = SetUtils.newLinkedHashSet(list);
// returns a linked hash set containing ["apple", "banana"]

Set emptySet = SetUtils.newLinkedHashSet(null);
// returns a new empty linked hash set
`

newLinkedHashSet

public static <E> Set<E> newLinkedHashSet(Iterator<E> elements)

Creates a new LinkedHashSet containing all elements from the provided Iterator.

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.

newLinkedHashSet

public static <E> Set<E> newLinkedHashSet(Collection<E> elements)

Creates a new LinkedHashSet containing all elements from the provided Collection.

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 list = Arrays.asList("apple", "banana", "apple");
Set set = SetUtils.newLinkedHashSet(list);
// returns a linked hash set containing ["apple", "banana"]

Set emptySet = SetUtils.newLinkedHashSet(Collections.emptyList());
// returns a new empty linked hash set
`

newLinkedHashSet

public static <E> Set<E> newLinkedHashSet(E... elements)

Creates a new LinkedHashSet 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 set1 = SetUtils.newLinkedHashSet("apple", "banana", "apple");
// returns a linked hash set containing ["apple", "banana"]

String[] fruits = {"apple", "banana"`;
Set set2 = SetUtils.newLinkedHashSet(fruits);
// returns a linked hash set containing ["apple", "banana"]

Set emptySet = SetUtils.newLinkedHashSet();
// returns a new empty linked hash set
}

newLinkedHashSet

public static <E> Set<E> newLinkedHashSet()

Creates a new, empty LinkedHashSet 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 set = SetUtils.newLinkedHashSet();
// returns a new empty linked hash set with default initial capacity (16) and load factor (0.75)
`

newLinkedHashSet

public static <E> Set<E> newLinkedHashSet(int initialCapacity)

Creates a new, empty LinkedHashSet 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 set = SetUtils.newLinkedHashSet(32);
// returns a new empty linked hash set with initial capacity of 32 and default load factor (0.75)
`

See Also

  • Sets

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally