Prev Next

Java / Java8 streams

Could not find what you were looking for? send us the question and we would be happy to answer your question.

Write a Java8 program to remove null from a stream.

Refer the below program.

import java.util.List;

public class FilterNull {

	public static void main(String[] args) {
		Stream<String> streamOfStrings = Stream.of("One", "Two", "Three", null, "Five", null);

		List<String> notEmptyStrings = streamOfStrings.filter(x -> x != null).collect(Collectors.toList());


What is a stream in Java8?

A stream represents a sequence of elements and supports a different kind of operations to perform computations upon those elements.

In Java 8, each Stream class represents a single-use sequence of data and supports several I/O operations.

Explain: IllegalStateException, stream has already been operated upon or closed.

A Stream should be operated o only once. A Stream implementation may throw IllegalStateException if it detects that the Stream is being reused.

Whenever a terminal operation is called on a Stream object, the instance gets consumed and closed. Therefore, it is allowed to perform a single operation that consumes a Stream, otherwise, an exception is encountered that states that the Stream has already been operated upon or closed.

public class LargestAndSmallest {

	public static void main(String[] args) {
		Stream<Integer> myIntStream = Stream.of(1, 2, 3, 44, 556, -1, 0, 2234);
		System.out.println("Largest = " + myIntStream.reduce(Integer::max));
		// The below line will throw java.lang.IllegalStateException

		System.out.println("Smallest = " + myIntStream.reduce(Integer::min));

How do I resolve IllegalStateException when Stream is reused?

Create new stream whenever required as shown below.

public class LargestAndSmallest {

	public static void main(String[] args) {
		Supplier<Stream<Integer>> myIntStream =  () -> Stream.of(1, 2, 3, 44, 556, -1, 0, 2234);
		System.out.println("Largest = " + myIntStream.get().reduce(Integer::max));
		System.out.println("Smallest = " + myIntStream.get().reduce(Integer::min));


Largest = Optional[2234]
Smallest = Optional[-1]
How to override equals method in Java8?

See the below code snippet.

public boolean equals (Object obj) {
return Optional.ofNullable(obj)
           .filter(that -> that instanceof Employee)
           .map(that -> (Employee) that)
           .filter(that -> this.fName.equals(that.fName))
           .filter(that -> this.lName.equals(that.lName))

Can you explain what is default method in Java 8?
Posted on Nov 19, 2018 by Rohit Sharma., K.

Default methods are the method defined in the interfaces with method body and using the default keyword. The implementing class may choose to override the default method or they may invoke the default method itself.

Default method can call methods from the interfaces they are enclosed in. Also in default method, this keyword refers to the declaring interface.

What type of variable be accessed in a lambda expression?

Inside a lambda expression, you can access final variables and also effectively final variables. Effectively final variables are initialized once however not marked final, second initialization lead to compilation error when used inside a lambda expression.

Is forEach a terminal operation?

Yes. After the operation is performed, the stream pipeline is considered consumed, and no longer be used.

Types of stream operations.

Stream operations are 2 types, intermediate and terminal operations, and together they form stream pipelines.

Intermediate operations return new are eam which allows you to call multiple operations in a form of a query. This operation does not get executed until a terminal operation is invoked. All Intermediate operations are lazy, so they are not executed until a result of a processing is actually needed, that is, traversal of the Stream does not start until the terminal operation of the pipeline is executed.

Examples of Stream intermediate operations include filter(), , map(), flatMap(), limit(), skip(), distinct(), sorted()peek().

Terminal operations are eagerly executed, and is always the last operation in a Stream pipeline. It can return a primitive type value (boolean, long), a concrete type (Optional value object), or nothing (void).

Examples of Terminal operations like Stream.forEach, IntStream.sum, may traverse the stream to produce a result or a side-effect. After the terminal operation is completed, the stream pipeline is considered consumed, and can no longer be used.

Types of intermediate operations in Stream.

There are 2 types, stateful and stateless.

Stateful intermediate operations maintain information (state) from a previous invocation to use again in a future invocation of the method. For example, Stream.distinct() needs to remember the previous elements it encountered, thus have to store state from previous passes. Another example of stateful intermediate operation is Streams.sorted().

Stateless intermediate operations doesn't have to maintain state that improves its performance and also help achieve parallelism. Examples of stateless intermediate operation include filter(), map(), findAny().

Explain stream pipeline and its components.

A steam pipeline is a sequence of aggregate operations. Examples of aggregate operations include filter and forEach.

A pipeline has the following components:

  • A source represented by collection, an array, a generator function, or an I/O channel.
  • 0 or more intermediate operations such as filter that produces a new stream.
  • A terminal operation, such as forEach, produces a non-stream result, such as a primitive value (boolean or double), a collection, or in the case of forEach, no value at all.
Differences Between Aggregate Operations such as forEach and Iterators.

stream.forEach and iterator works similar in terms of functionality. The below are the differences in how they operate.

Aggregate operations.Iterator.
They process elements from the stream not directly on the collection..Iterator directly process the collection.
Aggregate operation use internal iteration/internal delegation, JDK determines how to iterate the collection through stream . Iterator use external iteration in which your application code determines both what collection it iterates and how it iterates it. .
Parallel computing is possible by divide and conquer strategy.It supports only sequential iteration.
You may customize the behavior of an aggregate operation by passing behavior as lambda expression in parameter.Not applicable.
What are reduction operations?

Reduction operations are terminal operations such as average, sum, min, max, and count that return one value by combining the contents of a stream. There are 2 general-purpose reduction operations, Stream.reduce and Stream.collect.

Why lambda expression is needed?

Lambda expressions are used to define inline implementation of a functional interface, an interface with a only one (non static) method, that eliminates the need of anonymous class and brings a very simple and powerful functional programming capabilities into Java.

Explain limit and skip methods in Java8 streams.

The limit(long n) method of object performs stream reduction operation and returns a reduced stream of first n elements from the stream.

The skip(long n) method of object returns a stream of remaining elements after skipping first n elements.

Both methods limit and skip will throw an exception if n is negative

One programming question related to Java streams. I have an Array of Employees object (includes empName, sex, age, city). Can you write a Java program using Java streams concepts to find the number of Employees in each City? For example, Employees in Mumbai: 35 like that. Thanks in Advance.

Thanks for your question. Consider the below example. Kindly share your feedback/suggestions in the comment section.

package net.javapedia.streams;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

class Employee {
	public Employee(String empName, Sex sex, String city) {
		this.empName = empName; = sex; = city;

	String empName;

	public String getEmpName() {
		return empName;

	Sex sex;
	String city;

	public String getCity() {
		return city;


enum Sex {
	M, F;

public class GroupbyCountingExample {

	public static void main(String[] str) {

		List<Employee> myEmployees = new ArrayList<>();
		myEmployees.add(new Employee("Name1", Sex.M, "New York"));
		myEmployees.add(new Employee("Name2", Sex.F, "Dallas"));
		myEmployees.add(new Employee("Name3", Sex.F, "Charlotte"));
		myEmployees.add(new Employee("Name4", Sex.F, "Charlotte"));
		myEmployees.add(new Employee("Name5", Sex.M, "New York"));
		myEmployees.add(new Employee("Name5", Sex.M, "New York"));

		Map<String, Long> empCountByCity =
				.collect(Collectors.groupingBy(Employee::getCity, Collectors.counting()));

		for (String city : empCountByCity.keySet()) {
			System.out.println("Employee count in " + city + " :" + empCountByCity.get((city)));



Employee count in New York :3
Employee count in Charlotte :2
Employee count in Dallas :1

Explain Java8 Collectors partitioningBy method.

Java 8 Collectors.partitioningBy method accepts a predicate and returns a Collector by partitioning the element of the stream using Predicate so it will have 2 keys at most in Map. The key of the map can be only true and false and each key's value is a list of elements.

public static <T> Collector<T,?,Map<Boolean,List<T>>> 
                                         partitioningBy(Predicate<? super T> predicate)

Consider the below example. This program checks if each item of list1 exists in list2 and partition with predicate.

public class PartitioningByEx1 {

	public static void main(String[] args) {

		List<String> list1 = Arrays.asList("A", "B", "C", "D");
		List<String> list2 = Arrays.asList("A", "D", "E"); -> list2.contains(item)))
				.forEach((key, val) -> System.out.println(key + "-->>" + val));



false-->>[B, C]
true-->>[A, D]


Comments & Discussions