Class CollectionUtilities


  • public final class CollectionUtilities
    extends java.lang.Object
    Utility methods for operations on Collections.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.Collection<T> collect​(T... objs)
      Stores the given objects into a collection and returns it.
      static <T,​C extends java.util.Collection<T>>
      boolean
      equals​(C first, C second, java.util.function.BiPredicate<T,​T> equalityTest)
      Tests if two collections contain the same elements, using a custom equality test to determine if two elements are to be considered equals.
      static <T,​C extends java.util.Collection<T>>
      void
      join​(C first, C second, C result, java.util.function.BiPredicate<T,​T> equalityTest, java.util.function.BinaryOperator<T> joiner)
      Joins (union) two collections, using a custom equality test to determine if two elements are to be considered equals and thus joined together through the given joiner.
      static <T> int nullSafeCompare​(boolean nullFirst, T left, T right, java.util.Comparator<T> comparator)
      A null-safe comparison callback that invokes the given comparator only if the given objects are both non-null.
      • Methods inherited from class java.lang.Object

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

      • nullSafeCompare

        public static <T> int nullSafeCompare​(boolean nullFirst,
                                              T left,
                                              T right,
                                              java.util.Comparator<T> comparator)
        A null-safe comparison callback that invokes the given comparator only if the given objects are both non-null.
        Type Parameters:
        T - the type of compared objects
        Parameters:
        nullFirst - if true, null values will be treated as objects that appear before all non-null objects. This means that nullSafeCompare(true, null, obj, ...) returns -1. If false, the same invocation returns 1.
        left - the first element to compare
        right - the second element to compare
        comparator - the comparator to use if both objects are non-null
        Returns:
        an integer value determined by the ordering relation between objects, as in Comparator.compare(Object, Object)
      • equals

        public static <T,​C extends java.util.Collection<T>> boolean equals​(C first,
                                                                                 C second,
                                                                                 java.util.function.BiPredicate<T,​T> equalityTest)
        Tests if two collections contain the same elements, using a custom equality test to determine if two elements are to be considered equals. The two input collections are compared in an order-insensitive fashion.
        Type Parameters:
        T - the type of elements in the collections
        C - the type of the collections to test
        Parameters:
        first - the first collection
        second - the second collection
        equalityTest - the tester for equality of the objects within the collections
        Returns:
        true only if the collections contain exactly the same set of elements (order-insensitive)
      • join

        public static <T,​C extends java.util.Collection<T>> void join​(C first,
                                                                            C second,
                                                                            C result,
                                                                            java.util.function.BiPredicate<T,​T> equalityTest,
                                                                            java.util.function.BinaryOperator<T> joiner)
        Joins (union) two collections, using a custom equality test to determine if two elements are to be considered equals and thus joined together through the given joiner. All elements that, according to the given equalityTest are contained only in one of the input collections, will be added to the result as-is.
        Type Parameters:
        T - the type of elements in the collections
        C - the type of the collections to join
        Parameters:
        first - the first collection
        second - the second collection
        result - the collection to be used as result, where joined elements will be added
        equalityTest - the tester for equality of the objects within the collections
        joiner - the function that computes the join of two equal elements
      • collect

        @SafeVarargs
        public static <T> java.util.Collection<T> collect​(T... objs)
        Stores the given objects into a collection and returns it.
        Type Parameters:
        T - the type of objects
        Parameters:
        objs - the objects to store
        Returns:
        a (modifiable) collection containing the given objects