Class CollectionUtil


  • public final class CollectionUtil
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.List<T> asReadOnlyList​(java.util.List<T> list)
      Returns a read-only list.
      static <T,​K>
      java.util.Map<T,​K>
      asReadOnlyMap​(java.util.Map<T,​K> map)  
      static boolean equals​(java.lang.Object o1, java.lang.Object o2)
      Equality function that properly handles arrays, lists, maps, lists of arrays, and maps of arrays.
      static boolean equals​(java.lang.Object o1, java.lang.Object o2, boolean equalsIgnoreCase)
      Equality function that properly handles arrays, lists, maps, lists of arrays, and maps of arrays.
      static <T> int forceCompare​(java.lang.Object o1, java.lang.Object o2)
      Forces the compare of two comparable objects and removes any warnings generated by the compiler.
      static int hashCode​(java.lang.Object o)
      hashCode function that properly handles arrays, collections, maps, collections of arrays, and maps of arrays.
      static <T> java.util.Set<T> intersection​(java.util.Collection<T> c1, java.util.Collection<T> c2)
      Returns the intersection of two Collections as an unmodifiable set.
      static boolean isCaseInsensitiveMap​(java.util.Map<?,​?> map)
      Returns true if the given map is a case-insensitive map.
      static boolean isCaseInsensitiveSet​(java.util.Set<?> set)
      Returns true if the given set is a case-insensitive set.
      static <T> boolean isEmpty​(java.util.Collection<T> c)
      Determine if Collection is empty or not, protects against null being passed in.
      static <T> java.util.SortedMap<java.lang.String,​T> newCaseInsensitiveMap()
      Creates a case-insensitive map.
      static java.util.SortedSet<java.lang.String> newCaseInsensitiveSet()
      Creates a case-insensitive set.
      static <T> java.util.List<T> newList​(java.util.Collection<? extends T> c)
      Create a modifiable list from the Collection provided.
      static <T> java.util.List<T> newList​(T... arr)
      Create a modifiable list from the arguments.
      static java.util.Map<java.lang.String,​java.lang.String> newMap​(java.util.Properties properties)  
      static <T,​K>
      java.util.Map<T,​K>
      newMap​(T[] k, K[] v)
      Builds a map from two arrays.
      static <T,​K>
      java.util.Map<T,​K>
      newMap​(T k0, K v0)  
      static <T,​K>
      java.util.Map<T,​K>
      newMap​(T k0, K v0, T k1, K v1)  
      static <T,​K>
      java.util.Map<T,​K>
      newMap​(T k0, K v0, T k1, K v1, T k2, K v2)  
      static <T,​K>
      java.util.Map<T,​K>
      newMap​(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3)  
      static <T,​K>
      java.util.Map<T,​K>
      newMap​(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3, T k4, K v4)  
      static <T,​K>
      java.util.Map<T,​K>
      newMap​(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3, T k4, K v4, T k5, K v5)  
      static <T,​K>
      java.util.Map<T,​K>
      newMapFromLists​(java.util.List<T> keys, java.util.List<K> values)
      Converts two List to a map.
      static <T> java.util.List<T> newReadOnlyList​(java.util.Collection<? extends T> c)
      Creates an unmodifiable List from a collection.
      static <T> java.util.List<T> newReadOnlyList​(java.util.List<? extends T> list)
      Create an unmodifiable List based on the List passed in checks for null and returns an empty list if null is passed in.
      static <T> java.util.List<T> newReadOnlyList​(T... obj)
      Creates an unmodifiable List from a variable number arguments.
      static <T,​K>
      java.util.Map<T,​K>
      newReadOnlyMap​(java.util.Map<T,​K> map)  
      static <T> java.util.Map<T,​T> newReadOnlyMap​(T[][] kv)  
      static <T> java.util.Set<T> newReadOnlySet​(java.util.Collection<T> c)
      Creates an unmodifiable set from a Collection.
      static <T> java.util.Set<T> newReadOnlySet​(T... arr)
      Creates an unmodifiable set from a variable number arguments.
      static <T> java.util.Set<T> newSet​(java.util.Collection<T> c)
      Creates a set that can be modified from the Collection provided.
      static <T> java.util.Set<T> newSet​(T... arr)
      Creates a set that can be modified from the arguments.
      static <T extends java.lang.Object & java.lang.Comparable<? super T>>
      java.util.List<T>
      newSortedList​(java.util.Collection<? extends T> col)
      Create a modifiable sorted List based on the Collection provided.
      static <T> java.util.Collection<T> nullAsEmpty​(java.util.Collection<T> c)
      Protects from null and returns a new instance of HashSet.
      static <T> java.util.List<T> nullAsEmpty​(java.util.List<T> list)
      Protects from null and returns a new instance of ArrayList if the parameter list is null .
      static <T,​K>
      java.util.Map<T,​K>
      nullAsEmpty​(java.util.Map<T,​K> map)
      Protects from null and returns a new instance of HashMap if the parameter map is null.
      static <T> java.util.Set<T> nullAsEmpty​(java.util.Set<T> set)
      Protects from null and returns a new instance of HashSet if the parameter set is null.
      static <T> java.util.Set<T> union​(java.util.Collection<T> c1, java.util.Collection<T> c2)
      Returns the union of two Collections as an unmodifiable set.
      static <T> java.util.Collection<T> unique​(java.util.Collection<T> c)
      Use HashSet to create a unique Collection based on the one passed in.
      • Methods inherited from class java.lang.Object

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

      • newCaseInsensitiveSet

        public static java.util.SortedSet<java.lang.String> newCaseInsensitiveSet()
        Creates a case-insensitive set.
        Returns:
        An empty case-insensitive set
      • isCaseInsensitiveSet

        public static boolean isCaseInsensitiveSet​(java.util.Set<?> set)
        Returns true if the given set is a case-insensitive set.
        Parameters:
        set - The set. May be null.
        Returns:
        true if the given set is a case-insensitive set
      • newCaseInsensitiveMap

        public static <T> java.util.SortedMap<java.lang.String,​T> newCaseInsensitiveMap()
        Creates a case-insensitive map.
        Type Parameters:
        T - The object type of the map
        Returns:
        An empty case-insensitive map
      • isCaseInsensitiveMap

        public static boolean isCaseInsensitiveMap​(java.util.Map<?,​?> map)
        Returns true if the given map is a case-insensitive map.
        Parameters:
        map - The map. May be null.
        Returns:
        true if the given map is a case-insensitive map
      • nullAsEmpty

        public static <T> java.util.Collection<T> nullAsEmpty​(java.util.Collection<T> c)
        Protects from null and returns a new instance of HashSet. if the parameter c is null.
        Type Parameters:
        T - the type of the collection
        Parameters:
        c - collection to check
        Returns:
        if null new HashSet otherwise the parameter that was passed in or
      • nullAsEmpty

        public static <T,​K> java.util.Map<T,​K> nullAsEmpty​(java.util.Map<T,​K> map)
        Protects from null and returns a new instance of HashMap if the parameter map is null. Otherwise return the parameter that was passed in.
      • nullAsEmpty

        public static <T> java.util.Set<T> nullAsEmpty​(java.util.Set<T> set)
        Protects from null and returns a new instance of HashSet if the parameter set is null. Otherwise return the parameter that was passed in.
      • nullAsEmpty

        public static <T> java.util.List<T> nullAsEmpty​(java.util.List<T> list)
        Protects from null and returns a new instance of ArrayList if the parameter list is null . Otherwise return the parameter that was passed in.
      • isEmpty

        public static <T> boolean isEmpty​(java.util.Collection<T> c)
        Determine if Collection is empty or not, protects against null being passed in.
      • unique

        public static <T> java.util.Collection<T> unique​(java.util.Collection<T> c)
        Use HashSet to create a unique Collection based on the one passed in. The method protects against null. The returned Collection is unmodifiable.
      • newReadOnlyMap

        public static <T,​K> java.util.Map<T,​K> newReadOnlyMap​(java.util.Map<T,​K> map)
      • asReadOnlyMap

        public static <T,​K> java.util.Map<T,​K> asReadOnlyMap​(java.util.Map<T,​K> map)
      • newReadOnlyMap

        public static <T> java.util.Map<T,​T> newReadOnlyMap​(T[][] kv)
      • newMapFromLists

        public static <T,​K> java.util.Map<T,​K> newMapFromLists​(java.util.List<T> keys,
                                                                           java.util.List<K> values)
        Converts two List to a map. The order is important here because each key will map to one value.
      • newMap

        public static java.util.Map<java.lang.String,​java.lang.String> newMap​(java.util.Properties properties)
      • newMap

        public static <T,​K> java.util.Map<T,​K> newMap​(T k0,
                                                                  K v0)
      • newMap

        public static <T,​K> java.util.Map<T,​K> newMap​(T k0,
                                                                  K v0,
                                                                  T k1,
                                                                  K v1)
      • newMap

        public static <T,​K> java.util.Map<T,​K> newMap​(T k0,
                                                                  K v0,
                                                                  T k1,
                                                                  K v1,
                                                                  T k2,
                                                                  K v2)
      • newMap

        public static <T,​K> java.util.Map<T,​K> newMap​(T k0,
                                                                  K v0,
                                                                  T k1,
                                                                  K v1,
                                                                  T k2,
                                                                  K v2,
                                                                  T k3,
                                                                  K v3)
      • newMap

        public static <T,​K> java.util.Map<T,​K> newMap​(T k0,
                                                                  K v0,
                                                                  T k1,
                                                                  K v1,
                                                                  T k2,
                                                                  K v2,
                                                                  T k3,
                                                                  K v3,
                                                                  T k4,
                                                                  K v4)
      • newMap

        public static <T,​K> java.util.Map<T,​K> newMap​(T k0,
                                                                  K v0,
                                                                  T k1,
                                                                  K v1,
                                                                  T k2,
                                                                  K v2,
                                                                  T k3,
                                                                  K v3,
                                                                  T k4,
                                                                  K v4,
                                                                  T k5,
                                                                  K v5)
      • newMap

        public static <T,​K> java.util.Map<T,​K> newMap​(T[] k,
                                                                  K[] v)
        Builds a map from two arrays.
        Parameters:
        k - Array of keys.
        v - Array of values.
        Returns:
        a map based on the two arrays.
      • newSet

        public static <T> java.util.Set<T> newSet​(java.util.Collection<T> c)
        Creates a set that can be modified from the Collection provided.
      • newSet

        public static <T> java.util.Set<T> newSet​(T... arr)
        Creates a set that can be modified from the arguments.
      • newReadOnlySet

        public static <T> java.util.Set<T> newReadOnlySet​(T... arr)
        Creates an unmodifiable set from a variable number arguments.
      • newReadOnlySet

        public static <T> java.util.Set<T> newReadOnlySet​(java.util.Collection<T> c)
        Creates an unmodifiable set from a Collection.
      • union

        public static <T> java.util.Set<T> union​(java.util.Collection<T> c1,
                                                 java.util.Collection<T> c2)
        Returns the union of two Collections as an unmodifiable set.
      • intersection

        public static <T> java.util.Set<T> intersection​(java.util.Collection<T> c1,
                                                        java.util.Collection<T> c2)
        Returns the intersection of two Collections as an unmodifiable set.
      • newSortedList

        public static <T extends java.lang.Object & java.lang.Comparable<? super T>> java.util.List<T> newSortedList​(java.util.Collection<? extends T> col)
        Create a modifiable sorted List based on the Collection provided.
      • newList

        public static <T> java.util.List<T> newList​(java.util.Collection<? extends T> c)
        Create a modifiable list from the Collection provided. The return value is backed by an ArrayList.
      • newList

        public static <T> java.util.List<T> newList​(T... arr)
        Create a modifiable list from the arguments. The return value is backed by an ArrayList.
      • newReadOnlyList

        public static <T> java.util.List<T> newReadOnlyList​(java.util.List<? extends T> list)
        Create an unmodifiable List based on the List passed in checks for null and returns an empty list if null is passed in. This one insures that the order is maintained between lists.
      • newReadOnlyList

        public static <T> java.util.List<T> newReadOnlyList​(T... obj)
        Creates an unmodifiable List from a variable number arguments.
      • newReadOnlyList

        public static <T> java.util.List<T> newReadOnlyList​(java.util.Collection<? extends T> c)
        Creates an unmodifiable List from a collection.
      • asReadOnlyList

        public static <T> java.util.List<T> asReadOnlyList​(java.util.List<T> list)
        Returns a read-only list. The list is backed by the original so no copy is made.
        Type Parameters:
        T - The type of the list
        Parameters:
        list - The list or null.
        Returns:
        A read-only proxy on the original list.
      • forceCompare

        public static <T> int forceCompare​(java.lang.Object o1,
                                           java.lang.Object o2)
        Forces the compare of two comparable objects and removes any warnings generated by the compiler.
        Returns:
        Comparable the integer value of o1.compareTo(o2).
      • hashCode

        public static int hashCode​(java.lang.Object o)
        hashCode function that properly handles arrays, collections, maps, collections of arrays, and maps of arrays.
        Parameters:
        o - The object. May be null.
        Returns:
        the hashCode
      • equals

        public static boolean equals​(java.lang.Object o1,
                                     java.lang.Object o2)
        Equality function that properly handles arrays, lists, maps, lists of arrays, and maps of arrays.

        NOTE: For Sets, this relies on the equals method of the Set to do the right thing. This is a reasonable assumption since, in order for Sets to behave properly as Sets, their values must already have a proper implementation of equals. (Or they must be specialized Sets that define a custom comparator that knows how to do the right thing). The same holds true for Map keys. Map values, on the other hand, are compared (so Map values can be arrays).

        Parameters:
        o1 - The first object. May be null.
        o2 - The second object. May be null.
        Returns:
        true if the two objects are equal.
      • equals

        public static boolean equals​(java.lang.Object o1,
                                     java.lang.Object o2,
                                     boolean equalsIgnoreCase)
        Equality function that properly handles arrays, lists, maps, lists of arrays, and maps of arrays.

        NOTE: For Sets, this relies on the equals method of the Set to do the right thing. This is a reasonable assumption since, in order for Sets to behave properly as Sets, their values must already have a proper implementation of equals. (Or they must be specialized Sets that define a custom comparator that knows how to do the right thing). The same holds true for Map keys. Map values, on the other hand, are compared (so Map values can be arrays).

        Parameters:
        o1 - The first object. May be null.
        o2 - The second object. May be null.
        equalsIgnoreCase - if true the String and Character comparison is case-ignore
        Returns:
        true if the two objects are equal.
        Since:
        1.5