Class CollectionUtil


  • public class CollectionUtil
    extends Object
    Utilities for java collections
    Since:
    5.1.8
    Author:
    Tony Vaagenes, gjoranv
    • Constructor Detail

      • CollectionUtil

        public CollectionUtil()
    • Method Detail

      • mkString

        public static String mkString​(Collection<?> collection,
                                      String sep)
        Returns a String containing the string representation of all elements from the given collection, separated by the separator string.
        Parameters:
        collection - The collection
        sep - The separator string
        Returns:
        A string: elem(0) + sep + ... + elem(N)
      • mkString

        public static <T> String mkString​(Collection<T> collection,
                                          String start,
                                          String sep,
                                          String end)
        Returns a String containing the string representation of all elements from the given collection, using a start string, separator strings, and an end string.
        Type Parameters:
        T - The element type
        Parameters:
        collection - The collection
        start - The start string
        sep - The separator string
        end - The end string
        Returns:
        A string: start + elem(0) + sep + ... + elem(N) + end
      • equalContentsIgnoreOrder

        public static boolean equalContentsIgnoreOrder​(Collection<?> c1,
                                                       Collection<?> c2)
        Returns true if the contents of the two given collections are equal, ignoring order.
      • symmetricDifference

        public static <T> Set<T> symmetricDifference​(Collection<? extends T> c1,
                                                     Collection<? extends T> c2)
        Returns the symmetric difference between two collections, i.e. the set of elements that occur in exactly one of the collections.
      • filter

        public static <T> Collection<T> filter​(Collection<?> collection,
                                               Class<T> lowerBound)
        Returns the subset of elements from the given collection that can be cast to the reference type, defined by the given Class object.
      • first

        public static <T> T first​(Collection<T> collection)
        Returns the first element in a collection according to iteration order. Returns null if the collection is empty.
      • firstMatching

        public static <T> Optional<T> firstMatching​(T[] array,
                                                    Predicate<? super T> predicate)