Class CustomCollectors

    • Method Detail

      • distinctByKey

        public static <V,​K> Stream<V> distinctByKey​(@NonNull
                                                          Stream<V> stream,
                                                          @NonNull
                                                          Function<? super V,​? extends K> keyMapper)
        Produce a new stream with duplicates removed based on the provided keyMapper. When a duplicate key is encountered, the second item is used. The original sequencing is preserved if the input stream is sequential.
        Type Parameters:
        V - the item value for the streams
        K - the key type
        Parameters:
        stream - the stream to reduce
        keyMapper - the key function to use to find unique items
        Returns:
        a new stream
      • distinctByKey

        public static <V,​K> Stream<V> distinctByKey​(@NonNull
                                                          Stream<V> stream,
                                                          @NonNull
                                                          Function<? super V,​? extends K> keyMapper,
                                                          @NonNull
                                                          CustomCollectors.DuplicateHandler<K,​V> duplicateHander)
        Produce a new stream with duplicates removed based on the provided keyMapper. When a duplicate key is encountered, the provided duplicateHandler is used to determine which item to keep. The original sequencing is preserved if the input stream is sequential.
        Type Parameters:
        V - the item value for the streams
        K - the key type
        Parameters:
        stream - the stream to reduce
        keyMapper - the key function to use to find unique items
        duplicateHander - used to determine which of two duplicates to keep
        Returns:
        a new stream