Class SetUtils


  • public class SetUtils
    extends java.lang.Object
    Utility functions for {#link java.util.Set Set}s.
    Author:
    Evren Sirin
    • Constructor Summary

      Constructors 
      Constructor Description
      SetUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.Set<T> add​(T o, java.util.Set<T> set)
      Adds the given object to the set but saves memory space by allocating only the required amount for small sets.
      static <T> java.util.Set<T> binary​(T o1, T o2)  
      static <T> java.util.Set<T> create()
      Creates a default set.
      static <T> java.util.Set<T> create​(int initialSize)
      Creates a set with the given initial capacity.
      static <T> java.util.Set<T> create​(java.util.Collection<? extends T> elements)  
      static <T> java.util.Set<T> create​(T... elems)  
      static <T> java.util.Set<T> difference​(java.util.Collection<T> c1, java.util.Collection<? extends java.lang.Object> c2)  
      static <T> boolean equals​(java.util.Set<T> s1, java.util.Set<T> s2)  
      static <T> java.util.Set<T> intersection​(java.util.Collection<? extends java.util.Collection<? extends T>> coll)  
      static <T> java.util.Set<T> intersection​(java.util.Collection<? extends T> c1, java.util.Collection<? extends T> c2)  
      static boolean intersects​(java.util.Collection<?> c1, java.util.Collection<?> c2)  
      static <T> java.util.Set<T> remove​(java.lang.Object o, java.util.Set<T> set)  
      static <T> java.util.Set<T> singleton​(T o)  
      static boolean subset​(java.util.Set<?> sub, java.util.Set<?> sup)  
      static <T> java.util.Set<T> union​(java.util.Collection<? extends java.util.Collection<? extends T>> coll)  
      static <T> java.util.Set<T> union​(java.util.Collection<? extends T> c1, java.util.Collection<? extends T> c2)  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SetUtils

        public SetUtils()
    • Method Detail

      • add

        public static <T> java.util.Set<T> add​(T o,
                                               java.util.Set<T> set)
        Adds the given object to the set but saves memory space by allocating only the required amount for small sets. The idea is to use the specialized empty set and singleton set implementations (which are immutable) for the sets of size 0 and 1. If the set is empty a new singleton set is created, if set has one element we create a new set with two elements, otherwise we simply add the element to the given set.This technique is most useful if the expected set size is 0 or 1.
        Parameters:
        o -
        set -
        Returns:
        merge of set
      • remove

        public static <T> java.util.Set<T> remove​(java.lang.Object o,
                                                  java.util.Set<T> set)
      • singleton

        public static final <T> java.util.Set<T> singleton​(T o)
      • binary

        public static final <T> java.util.Set<T> binary​(T o1,
                                                        T o2)
      • union

        public static <T> java.util.Set<T> union​(java.util.Collection<? extends java.util.Collection<? extends T>> coll)
        Parameters:
        coll - A Collection of sets
        Returns:
        the union of all the sets given in a collection.
      • union

        public static <T> java.util.Set<T> union​(java.util.Collection<? extends T> c1,
                                                 java.util.Collection<? extends T> c2)
        Parameters:
        c1 - A Collection of sets
        c2 - A Collection of sets
        Returns:
        the union of two collections
      • intersection

        public static <T> java.util.Set<T> intersection​(java.util.Collection<? extends java.util.Collection<? extends T>> coll)
        Parameters:
        coll - A Collection of sets
        Returns:
        the intersection of all the collections given in a collection.
      • intersection

        public static <T> java.util.Set<T> intersection​(java.util.Collection<? extends T> c1,
                                                        java.util.Collection<? extends T> c2)
        Parameters:
        c1 - A Collection of sets
        c2 - A Collection of sets
        Returns:
        the intersection of two collections
      • intersects

        public static boolean intersects​(java.util.Collection<?> c1,
                                         java.util.Collection<?> c2)
        Parameters:
        c1 -
        c2 -
        Returns:
        true if two collections have any elements in common
      • subset

        public static boolean subset​(java.util.Set<?> sub,
                                     java.util.Set<?> sup)
        Parameters:
        sub -
        sup -
        Returns:
        true if one set is subset of another one
      • equals

        public static <T> boolean equals​(java.util.Set<T> s1,
                                         java.util.Set<T> s2)
        Parameters:
        s1 -
        s2 -
        Returns:
        true if one set is equal of another one
      • difference

        public static <T> java.util.Set<T> difference​(java.util.Collection<T> c1,
                                                      java.util.Collection<? extends java.lang.Object> c2)
        Parameters:
        c1 -
        c2 -
        Returns:
        the difference of two sets. All the elements of second set is removed from the first set
      • create

        @SafeVarargs
        public static <T> java.util.Set<T> create​(T... elems)
        Parameters:
        elems -
        Returns:
        a new set containing all the elements in the array
      • create

        public static <T> java.util.Set<T> create​(int initialSize)
        Creates a set with the given initial capacity.
        Parameters:
        initialSize - is the initial size of the set.
        Returns:
        a fresh set resilient to concurrency.
        Since:
        2.6.0
      • create

        public static <T> java.util.Set<T> create()
        Creates a default set.
        Returns:
        a fresh set resilient to concurrency.
        Since:
        2.6.0
      • create

        public static <T> java.util.Set<T> create​(java.util.Collection<? extends T> elements)
        Parameters:
        elements -
        Returns:
        a new set containing all the elements in the collection