Developed with


Stream API

The course is part of this learning path

Start course

This training course provides you with a deep dive into the Java Stream API. The Java Stream API is a functional stream processing API and is used to define the logic of a task in a declarative way.

Learning Objectives

What you'll learn:

  • What the Stream API provides and when and why you should use it
  • How to use the Stream API to process elements within a collection
  • How to filter and find elements within a Stream
  • How to group and gather statistics on elements within a Stream


  • A basic understanding of the Java programming language
  • A basic understanding of software development
  • A basic understanding of the software development life cycle

Intended Audience

  • Software Engineers interested in advancing their Java skills
  • Software Architects interested in using advanced features of Java to design and build both applications and frameworks
  • Anyone interested in advanced Java application development and associated tooling
  • Anyone interested in understanding the advanced areas and features of the Java SDK

- [Jeremy] Okay, welcome back. In this lecture, we'll explore the concept of collectors and how to work with them within Java. In particular, we'll review the following topics: using different ways to collect the items from a stream, grouping elements within a stream and gathering statistics about the numeric property of elements within a stream. At the conclusion of this lecture, you should be able to perform each of the items listed above. Take a moment to write yourself on each of these items on a scale of one, three to five. At the conclusion of this lecture, these objectives will be reviewed. You should write yourself again to see how much benefit you received from this lecture. To begin with, collectors transform elements within a stream into a mutable result. They are used for accumulating elements into a collection. They can be used for grouping elements by element property. For example, flights by destination. They can be used for summarizing elements into a single value e.g. minimum/maximum, average or count and can be used for partitioning elements, for example splitting a stream into parts depending on criteria and to mediate operations can be defined on a stream of elements to define operations that need to take place on the elements within the stream thereby creating a pipeline of operations. A tubital operation can perform operations on a stream but also creates a mutable result. The collect method of the stream class accepts a parameter of type collector. Different implementations of the collector interface are capable of collecting elements in a stream into different types of objects. Often, the result of the intermediate operations on a stream will be collected into a collection implementation but other operations that might be defined by the collector are grouping, summarizing and partitioning of elements as will be explained in this lecture. The collectors class contains a list of factory methods that can be used to obtain commonly used collector implementations. This lesson covers the use of some of these collectors. Keep in mind that a lot of overloaded versions of these methods are available on the the Collector's Class. If you still can't find one, they will provide you with the logic you need. You can always write your own custom collector by implementing the Collector Interface yourself. Creating collections from a stream. The contents of a stream can be collected into a collection implementation. Convenience factory methods for collecting into a list or set implementation are available. But it is also possible to specify the specific collection implementation you need. Grouping within streams. The grouping By collector requires a function that specifies the criteria on which the grouping should take place. In the example here, the destination value of the flight object is read by using the method reference and elements are then grouped by the result of this method invocation. Joining elements. Joining allows for the joining of all the elements within a stream into a string of value specifying the separated that is placed in between the elements and the prefix and suffix of the final result. The example showing here creates a comma separated list of all the distinct destinations for which flights are scheduled. Partitioning. Partitioning is basically a group by operation. But this time the result will only be two lists. One list of the elements that conforms to the predicate of the operation and one list of elements that don't. Counting and summing. We've seen the counting operation earlier that counts the amount of elements available in the stream. Collectors for summing numeric values within a stream are also available by default. However, when you are interested in additional statistics about a particular numeric value within the stream, you might want to consider any of these summarizing collectors. The result object not only contains the sum of the numeric value in the stream but also the max, min and average. Okay, before we complete this lecture, pause this video and consider the following questions to test yourself on the content that we've just reviewed. Write down your answers for each question and then resume the video to compare answers. Okay, the answers to the above questions are: one, implementations of the collector interface used to gather the elements in a stream into a mutable value, two, groupingBy divides the counting into one or more groups PartitioningBy divides the element in one or two groups depending on whether the element meets criteria.

About the Author
Jeremy Cook
Content Lead Architect
Learning Paths

Jeremy is a Content Lead Architect and DevOps SME here at Cloud Academy where he specializes in developing DevOps technical training documentation.

He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 25+ years. In recent times, Jeremy has been focused on DevOps, Cloud (AWS, GCP, Azure), Security, Kubernetes, and Machine Learning.

Jeremy holds professional certifications for AWS, GCP, and Kubernetes.

Covered Topics