Class CollectionHelper


  • public class CollectionHelper
    extends java.lang.Object
    A collection of static methods to help with using java.util.Collection derivates. This is an addition to Collections.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> void copySelective​(java.util.Collection<? extends T> source, java.util.Collection<? super T> target, Predicate<T> filter)
      Copies all elements that match a predicate.
      static <T> java.util.List<T> selectAsList​(java.util.Collection<? extends T> source, Predicate<T> filter)
      Retrieves all elements from a collection that match a predicate.
      static <T> T selectFirst​(java.util.Collection<? extends T> source, Predicate<T> filter)
      Retrieves the first element from a collection that matches a predicate.
      • Methods inherited from class java.lang.Object

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

      • CollectionHelper

        public CollectionHelper()
    • Method Detail

      • copySelective

        public static <T> void copySelective​(java.util.Collection<? extends T> source,
                                             java.util.Collection<? super T> target,
                                             Predicate<T> filter)
        Copies all elements that match a predicate. Every element of the first collection for which the predicate holds (i.e. returns true) is added to the second collection.
      • selectAsList

        public static <T> java.util.List<T> selectAsList​(java.util.Collection<? extends T> source,
                                                         Predicate<T> filter)
        Retrieves all elements from a collection that match a predicate. The result will be all elements of the source collection for which the predicate is true, retrieved in normal iteration order.
        Type Parameters:
        T - The type of elements we are interested in.
        Parameters:
        source - A collection containing elements we are interested in. Not null. Can be empty.
        filter - The predicate determining if an element is to be retrieved. Not null.
        Returns:
        A list containing all matching elements in iteration order. Never null. Can be empty.
      • selectFirst

        public static <T> T selectFirst​(java.util.Collection<? extends T> source,
                                        Predicate<T> filter)
        Retrieves the first element from a collection that matches a predicate.
        Type Parameters:
        T - The type of elements we are interested in.
        Parameters:
        source - A collection containing elements we are interested in. Not null. Can be empty.
        filter - The predicate determining if an element is to be retrieved. Not null.
        Returns:
        The first matching element or null if no element matches.