Class CollectionHelper


  • public final class CollectionHelper
    extends Object
    Helper class for simplifying lambda expression usage on collections and arrays.
    • Constructor Detail

      • CollectionHelper

        public CollectionHelper()
    • Method Detail

      • firstOrDefault

        public static <T> T firstOrDefault​(Collection<T> collection,
                                           Predicate<T> condition)
        Returns the first element of the collection that satisfies the given condition, or null if no such element exists. If the condition is null, the first element of the collection is returned.
        Type Parameters:
        T - The type of the collection elements
        Parameters:
        collection - The collection to be searched
        condition - The condition to be applied
        Returns:
        The first element of the collection matching the condition, or null
      • firstOrDefault

        public static <T> T firstOrDefault​(T[] array,
                                           Predicate<T> condition)
        Returns the first element of the array that satisfies the given condition, or null if no such element exists. If the condition is null, the first element of the array is returned.
        Type Parameters:
        T - The type of the array elements
        Parameters:
        array - The array to be searched
        condition - The condition to be applied
        Returns:
        The first element of the array matching the condition, or null
      • where

        public static <T> List<T> where​(Collection<T> collection,
                                        Predicate<T> filter)
        Selects the list of collection elements that satisfy the given filter. If the filter is null, all the collection elements are returned.
        Type Parameters:
        T - The type of collection elements
        Parameters:
        collection - The collection to be filtered
        filter - The filter to be applied
        Returns:
        A list of elements satisfying the filter
      • where

        public static <T> List<T> where​(T[] array,
                                        Predicate<T> filter)
        Selects the list of array elements that satisfy the given filter. If the filter is null, all the array elements are returned.
        Type Parameters:
        T - The type of array elements
        Parameters:
        array - The array to be filtered
        filter - The filter to be applied
        Returns:
        A list of elements satisfying the filter