Class SetUtils

  • All Implemented Interfaces:
    Utils

    public abstract class SetUtils
    extends java.lang.Object
    implements Utils
    The utilities class for Java Set
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    Sets
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isSet​(java.lang.Iterable<?> elements)
      Checks whether the specified Iterable is an instance of Set.
      static <E> java.util.Set<E> newHashSet()
      Creates a new, empty HashSet with the default initial capacity and load factor.
      static <E> java.util.Set<E> newHashSet​(int initialCapacity)
      Creates a new, empty HashSet with the specified initial capacity and default load factor.
      static <E> java.util.Set<E> newHashSet​(int initialCapacity, float loadFactor)
      Creates a new, empty HashSet with the specified initial capacity and load factor.
      static <E> java.util.Set<E> newHashSet​(E... elements)
      Creates a new HashSet containing all elements from the provided varargs array.
      static <E> java.util.Set<E> newHashSet​(java.lang.Iterable<E> elements)
      Creates a new HashSet containing all elements from the provided Iterable.
      static <E> java.util.Set<E> newHashSet​(java.util.Collection<E> elements)
      Creates a new HashSet containing all elements from the provided Collection.
      static <E> java.util.Set<E> newLinkedHashSet()
      Creates a new, empty LinkedHashSet with the default initial capacity and load factor.
      static <E> java.util.Set<E> newLinkedHashSet​(int initialCapacity)
      Creates a new, empty LinkedHashSet with the specified initial capacity and default load factor.
      static <E> java.util.Set<E> newLinkedHashSet​(int initialCapacity, float loadFactor)
      Creates a new, empty LinkedHashSet with the specified initial capacity and load factor.
      static <E> java.util.Set<E> newLinkedHashSet​(E... elements)
      Creates a new LinkedHashSet containing all elements from the provided varargs array.
      static <E> java.util.Set<E> newLinkedHashSet​(java.lang.Iterable<E> elements)
      Creates a new LinkedHashSet containing all elements from the provided Iterable.
      static <E> java.util.Set<E> newLinkedHashSet​(java.util.Collection<E> elements)
      Creates a new LinkedHashSet containing all elements from the provided Collection.
      static <E> java.util.Set<E> newLinkedHashSet​(java.util.Iterator<E> elements)
      Creates a new LinkedHashSet containing all elements from the provided Iterator.
      static <E> java.util.Set<E> of​(E... elements)
      Creates an unmodifiable Set from the given varargs array of elements.
      static <E> java.util.Set<E> ofSet​(E... elements)
      Creates an unmodifiable Set from the given varargs array of elements.
      static <E> java.util.Set<E> ofSet​(java.lang.Iterable<E> elements)
      Creates an unmodifiable Set from the given Iterable.
      static <T> java.util.Set<T> ofSet​(java.util.Collection<T> elements)
      Creates an unmodifiable Set from the given Collection.
      static <T> java.util.Set<T> ofSet​(java.util.Collection<T> elements, T... others)
      Creates an unmodifiable Set from the given Collection and additional varargs elements.
      static <E> java.util.Set<E> ofSet​(java.util.Enumeration<E> elements)
      Creates an unmodifiable Set from the given Enumeration.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isSet

        public static boolean isSet​(@Nullable
                                    java.lang.Iterable<?> elements)
        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<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 - the Iterable to check, may be null
        Returns:
        true if the given iterable is a Set; otherwise, false
      • of

        @Nonnull
        public static <E> java.util.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<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 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<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 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<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 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<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 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<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 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<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 empty
        others - 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 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<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 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<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 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<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, 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<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, 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<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, 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<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 set
        loadFactor - 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 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<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 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.

        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 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<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 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<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, 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<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, 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<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, empty LinkedHashSet 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 set
        loadFactor - the load factor of the returned set
        Returns:
        a new empty LinkedHashSet with the specified initial capacity and load factor