Class CollectionUtil


  • @Internal
    public final class CollectionUtil
    extends Object
    Simple utility to work with Java collections.
    • Field Detail

      • MAX_ARRAY_SIZE

        public static final int MAX_ARRAY_SIZE
        A safe maximum size for arrays in the JVM.
        See Also:
        Constant Field Values
    • Method Detail

      • isNullOrEmpty

        public static boolean isNullOrEmpty​(Collection<?> collection)
        Returns true if the given collection is null or empty.
      • isEmptyOrAllElementsNull

        public static boolean isEmptyOrAllElementsNull​(Collection<?> collection)
        Returns true if the given collection is empty or contains only null elements.
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(Map<?,​?> map)
      • ofNullable

        public static <T> Set<T> ofNullable​(@Nullable
                                            T obj)
      • partition

        public static <T> Collection<List<T>> partition​(Collection<T> elements,
                                                        int numBuckets)
        Partition a collection into approximately n buckets.
      • iterableToList

        public static <E> List<E> iterableToList​(@Nullable
                                                 Iterable<E> iterable)
        Collects the elements in the Iterable in a List. If the iterable argument is null, this method returns an empty list.
      • iteratorToList

        public static <E> List<E> iteratorToList​(@Nullable
                                                 Iterator<E> iterator)
        Collects the elements in the Iterator in a List. If the iterator argument is null, this method returns an empty list.
      • entry

        public static <K,​V> Map.Entry<K,​V> entry​(K k,
                                                             V v)
        Returns an immutable Map.Entry.
      • map

        @SafeVarargs
        public static <K,​V> Map<K,​V> map​(Map.Entry<K,​V>... entries)
        Returns an immutable Map from the provided entries.
      • newHashMapWithExpectedSize

        public static <K,​V> HashMap<K,​V> newHashMapWithExpectedSize​(int expectedSize)
        Creates a new HashMap of the expected size, i.e. a hash map that will not rehash if expectedSize many keys are inserted, considering the load factor.
        Type Parameters:
        K - the type of keys maintained by this map.
        V - the type of mapped values.
        Parameters:
        expectedSize - the expected size of the created hash map.
        Returns:
        a new hash map instance with enough capacity for the expected size.
      • newLinkedHashMapWithExpectedSize

        public static <K,​V> LinkedHashMap<K,​V> newLinkedHashMapWithExpectedSize​(int expectedSize)
        Creates a new LinkedHashMap of the expected size, i.e. a hash map that will not rehash if expectedSize many keys are inserted, considering the load factor.
        Type Parameters:
        K - the type of keys maintained by this map.
        V - the type of mapped values.
        Parameters:
        expectedSize - the expected size of the created hash map.
        Returns:
        a new hash map instance with enough capacity for the expected size.
      • newHashSetWithExpectedSize

        public static <E> HashSet<E> newHashSetWithExpectedSize​(int expectedSize)
        Creates a new HashSet of the expected size, i.e. a hash set that will not rehash if expectedSize many unique elements are inserted, considering the load factor.
        Type Parameters:
        E - the type of elements stored by this set.
        Parameters:
        expectedSize - the expected size of the created hash map.
        Returns:
        a new hash map instance with enough capacity for the expected size.
      • newLinkedHashSetWithExpectedSize

        public static <E> LinkedHashSet<E> newLinkedHashSetWithExpectedSize​(int expectedSize)
        Creates a new LinkedHashSet of the expected size, i.e. a hash set that will not rehash if expectedSize many unique elements are inserted, considering the load factor.
        Type Parameters:
        E - the type of elements stored by this set.
        Parameters:
        expectedSize - the expected size of the created hash map.
        Returns:
        a new hash map instance with enough capacity for the expected size.
      • subTypeCast

        public static <T> Collection<T> subTypeCast​(Collection<? super T> collection)
        Casts the given collection to a subtype. This is an unchecked cast that can lead to runtime exceptions.
        Type Parameters:
        T - the subtype to cast to.
        Parameters:
        collection - the collection to cast.
        Returns:
        the collection unchecked-cast to a subtype.
      • checkedSubTypeCast

        public static <T> Collection<T> checkedSubTypeCast​(Collection<? super T> collection,
                                                           Class<T> subTypeClass)
        Casts the given collection to a subtype. This is a checked cast.
        Type Parameters:
        T - the subtype to cast to.
        Parameters:
        collection - the collection to cast.
        subTypeClass - the class of the subtype to cast to.
        Returns:
        the collection checked and cast to a subtype.