Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of Java's Stream#Peek method in Linq C#?

I have been used to using Java's Stream#Peek method so much as it's a useful method to debug intermediate stream operation. For those of you who are not familiar with the Stream#Peek method, below shows the definition of it:

Stream<T> peek(Consumer<? super T> action)

Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. This is an intermediate operation.

Consider this simple example below:

List<Integer> integerList = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
List<Integer> result = integerList.stream()
                                  .filter(i -> i % 2 == 0)
                                  .peek(System.out::println)
                                  .collect(Collectors.toList());

With the Stream#Peek method, this should basically allow me to print all the even numbers to the console so that I can test to see if that's what I would expect.

I have tried to find an answer to the question at hand but can't seem to find a similar method in C#, does anyone know the equivalent of Java's Stream#Peek or some other method with similar behavior?

like image 771
Ousmane D. Avatar asked Jul 07 '17 21:07

Ousmane D.


People also ask

What is stream in JDK 8?

Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.

Is Java stream same as Linq C#?

Streams are similar to LINQ in many ways, but there are still some differences. Because Java only started using streams in 2014, the way in which they apply it to simplify querying sets of data can seem a little bit half-heartedly to a . NET developer (since LINQ was already introduced in 2008).

Does Java 8 have streams?

Java 8 offers the possibility to create streams out of three primitive types: int, long and double. As Stream<T> is a generic interface, and there is no way to use primitives as a type parameter with generics, three new special interfaces were created: IntStream, LongStream, DoubleStream.

Is Java stream same as loop?

Streams can be a replacement for looping because they allow for the processing of a sequence of data (similarly to a loop).


1 Answers

There is no equivalent of Peek in LINQ - i.e. there is no method which performs some action and returns source elements. There is a ForEach method in List class which performs an operation on each element, but it does not return source elements, and as already said, it's not an IEnumerable extension.

But you can easily write your own extension

public static IEnumerable<T> Peek<T>(this IEnumerable<T> source, Action<T> action)
{
    if (source == null) throw new ArgumentNullException(nameof(source));
    if (action == null) throw new ArgumentNullException(nameof(action));

    return Iterator();

    IEnumerable<T> Iterator() // C# 7 Local Function
    {
       foreach(var item in source)
       {
           action(item);
           yield return item;
       }
    }
}
like image 146
Sergey Berezovskiy Avatar answered Nov 15 '22 17:11

Sergey Berezovskiy