Java Stream collect() operation with examples

 240 total views,  1 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream collect() operation with examples.

Stream collect() is a reduction operation performed on the stream of elements. It applies a certain function on the stream of elements to generate the final output.

Stream collect() is mainly used for,

  1. converting the stream of elements into a collection interface such as List, Set or Map.
  2. Collect the elements from the stream and store it in the collection.
  3. Perform count, min, max, and sum.
  4. Perform aggregate operations such as GroupBy
  5. Concatenate the elements of the stream into a single result using Joining.

All the above Stream collect operations are provided via an API called Collectors.


The Collectors API has all required operations that are supported by Stream collect() operation.


<R> R collect(Supplier<R> supplier,BiConsumer<R,? super T> accumulator,BiConsumer<R,R> combiner)
<R,A> R collect(Collector<? super T,A,R> collector)


Let us see detailed examples of Stream collect() operation.

  1. Accumulate Stream of elements in a list
  2. Accumulate Stream of elements in a Set
  3. Accumulate Stream of elements in a Map
  4. Convert a Stream to HashMap
  5. Convert a Stream to LinkedHashMap
  6. Collectors.toCollection
  7. Collectors.counting()
  8. Collectors.maxBy()
  9. Collectors.minBy()
  10. Collectors.joining()
  11. Collectors.summingInt()/summingLong()/summingDouble()
  12. Collectors.summarizingInt()
  13. Collectors.summarizingLong()
  14. Collectors.summarizingDouble()
  15. Collectors averagingDouble()/averagingInt/averagingLong
  16. Collectors.partitioningBy()
  17. Collectors.groupingBy()


References :Official documentation

Check out this link to learn more about Java 8 features and stream operations

I hope you like this article. Thanks for reading and Please leave us a comment below!

Tags :

About the Author


Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us ( and give a thumbs up :) Feel free to contact me for any queries!.