Java Stream reduce to different type Example (List of Employee to a String)

Many times we have a list of particular object & we want to reduce it to some different type. Here we will take example of List of Employee object. We will reduce it to a single String using reduce with accumulator, identity & combiner.

Reduction operation:

  • Iterate list of Employee objects.
  • For each Employee object, create a String by concatenating Name & Department.
  • Then concatenate all such strings of all employee into single String.

We will use <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner) function with 3 arguments.

Output:

 

Leave a Reply

Your email address will not be published. Required fields are marked *