Class CollectionUtils


  • public final class CollectionUtils
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.SortedSet EMPTY_SORTED_SET
      Left raw deliberately, as java.util.Collections leaves EMPTY_LIST and EMPTY_SET raw: a raw empty constant can be assigned to a SortedSet of any element type, where a SortedSet<?> could not be assigned to anything.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> void add​(java.util.Collection<? super T> c, T o)  
      static <T> java.util.Collection<T> attemptClone​(java.util.Collection<T> c)
      Attempts to find a public clone() method or a copy constructor, in that order, and calls what it finds.
      static <K,​V>
      java.util.Map<K,​V>
      attemptClone​(java.util.Map<K,​V> m)
      Attempts to find a public clone() method or a copy constructor, in that order, and calls what it finds.
      static boolean isCollectionsWrapper​(java.lang.Class<?> cl)  
      static boolean isCollectionsWrapper​(java.util.Collection<?> c)  
      static boolean isCollectionsWrapper​(java.util.Map<?,​?> m)  
      static boolean isSynchronizedWrapper​(java.lang.Class<?> cl)  
      static boolean isSynchronizedWrapper​(java.util.Collection<?> c)  
      static boolean isSynchronizedWrapper​(java.util.Map<?,​?> m)  
      static boolean isUnmodifiableWrapper​(java.lang.Class<?> cl)  
      static boolean isUnmodifiableWrapper​(java.util.Collection<?> c)  
      static boolean isUnmodifiableWrapper​(java.util.Map<?,​?> m)  
      static <T> java.util.Collection<T> narrowSynchronizedCollection​(java.util.Collection<T> c)  
      static <K,​V>
      java.util.Map<K,​V>
      narrowSynchronizedMap​(java.util.Map<K,​V> m)  
      static <T> java.util.Collection<T> narrowUnmodifiableCollection​(java.util.Collection<T> c)  
      static <K,​V>
      java.util.Map<K,​V>
      narrowUnmodifiableMap​(java.util.Map<K,​V> m)  
      static void remove​(java.util.Collection<?> c, java.lang.Object o)  
      static int size​(java.lang.Object o)  
      • Methods inherited from class java.lang.Object

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

      • EMPTY_SORTED_SET

        public static final java.util.SortedSet EMPTY_SORTED_SET
        Left raw deliberately, as java.util.Collections leaves EMPTY_LIST and EMPTY_SET raw: a raw empty constant can be assigned to a SortedSet of any element type, where a SortedSet<?> could not be assigned to anything. The initializer is parameterized so that the rawness costs no unchecked operation.
    • Method Detail

      • isCollectionsWrapper

        public static boolean isCollectionsWrapper​(java.lang.Class<?> cl)
      • isCollectionsWrapper

        public static boolean isCollectionsWrapper​(java.util.Collection<?> c)
      • isCollectionsWrapper

        public static boolean isCollectionsWrapper​(java.util.Map<?,​?> m)
      • isSynchronizedWrapper

        public static boolean isSynchronizedWrapper​(java.lang.Class<?> cl)
      • isSynchronizedWrapper

        public static boolean isSynchronizedWrapper​(java.util.Collection<?> c)
      • isSynchronizedWrapper

        public static boolean isSynchronizedWrapper​(java.util.Map<?,​?> m)
      • isUnmodifiableWrapper

        public static boolean isUnmodifiableWrapper​(java.lang.Class<?> cl)
      • isUnmodifiableWrapper

        public static boolean isUnmodifiableWrapper​(java.util.Collection<?> c)
      • isUnmodifiableWrapper

        public static boolean isUnmodifiableWrapper​(java.util.Map<?,​?> m)
      • narrowUnmodifiableCollection

        public static <T> java.util.Collection<T> narrowUnmodifiableCollection​(java.util.Collection<T> c)
      • narrowSynchronizedCollection

        public static <T> java.util.Collection<T> narrowSynchronizedCollection​(java.util.Collection<T> c)
      • narrowUnmodifiableMap

        public static <K,​V> java.util.Map<K,​V> narrowUnmodifiableMap​(java.util.Map<K,​V> m)
      • narrowSynchronizedMap

        public static <K,​V> java.util.Map<K,​V> narrowSynchronizedMap​(java.util.Map<K,​V> m)
      • attemptClone

        public static <T> java.util.Collection<T> attemptClone​(java.util.Collection<T> c)
                                                        throws java.lang.NoSuchMethodException
        Attempts to find a public clone() method or a copy constructor, in that order, and calls what it finds. If neither is available, throws a NoSuchMethodException. Unchecked is suppressed rather than avoided here. Every route to the copy -- Object clone(), Method.invoke(...), Constructor.newInstance(...) -- is typed to return Object, so the element type cannot be checked at the point of the cast, only preserved. A copy of a Collection has the element type of the original, which is what the signature says; the compiler simply cannot see it through reflection.
        Throws:
        java.lang.NoSuchMethodException
      • attemptClone

        public static <K,​V> java.util.Map<K,​V> attemptClone​(java.util.Map<K,​V> m)
                                                                 throws java.lang.NoSuchMethodException
        Attempts to find a public clone() method or a copy constructor, in that order, and calls what it finds. If neither is available, throws a NoSuchMethodException. Unchecked is suppressed rather than avoided here. Every route to the copy -- Object clone(), Method.invoke(...), Constructor.newInstance(...) -- is typed to return Object, so the element type cannot be checked at the point of the cast, only preserved. A copy of a Map has the key and value types of the original, which is what the signature says; the compiler simply cannot see it through reflection.
        Throws:
        java.lang.NoSuchMethodException
      • add

        public static <T> void add​(java.util.Collection<? super T> c,
                                   T o)
      • remove

        public static void remove​(java.util.Collection<?> c,
                                  java.lang.Object o)
      • size

        public static int size​(java.lang.Object o)