Class CollectionUtils

  • public class CollectionUtils
    extends Object
    A utility class that provides re-usable functionality that relates to Java collections.
    Guus der Kinderen,
    • Constructor Detail

      • CollectionUtils

        public CollectionUtils()
    • Method Detail

      • distinctByKey

        public static <T> Predicate<T> distinctByKey​(Function<? super T,​Object> keyExtractor)
        Returns a stateful stream filter that, once applied to a stream, returns a stream consisting of the distinct elements (according to the specified key).

        The implementation of Stream.distinct() can be used to return a stream that has distinct elements, based on the implementation of Object.equals(Object). That implementation does not allow to filter a stream based on one property of each object. The implementation of provided by this method does.

        Type Parameters:
        T - Stream element type.
        keyExtractor - A function to extract the desired key from the stream objects (cannot be null).
        A filter
        See Also:
        Stack Overflow: Java Lambda Stream Distinct() on arbitrary key?