In this post, we will see Java8 Streams Interview questions.

What is the difference between a Stream and a Collection in Java 8

  • Stream represents a sequence of elements and supports various operations to perform computations upon those elements. It is a more powerful and flexible version of the Enumeration and Iterator interfaces.
  • A Collection represents a group of elements and supports various operations to add, remove, and retrieve elements. It is a more general interface that encompasses List, Set, and Queue interfaces.
  • The main difference between a Stream and a Collection is that a Stream is designed for functional-style operations and Collection is designed for storage and retrieval of elements.
  • A Stream is a one-time use object, it can be consumed once and it’s not possible to use it again. Streams are designed to perform operations on a large number of elements and they provide the advantage of lazy evaluation. This means that the operations are performed only when the terminal operation is called.
  • Collections are designed for storage and retrieval of elements and they provide more general operations such as add, remove, and retrieve.
  • You can convert a Collection to a Stream using the stream() method. In short, a Stream is a powerful and flexible way to perform functional-style operations on a sequence of elements, while a Collection is a more general interface for storing and retrieving elements.
List<String> words = Arrays.asList("Java", "Streams", "are", "powerful");
Stream<String> stream = words.stream();

How do you create a stream in Java 8

Please click on above post for more details: https://javasavvy.com/java8-streams-features-and-stream-creation-exampls/

How does the lazy evaluation feature of streams work?

Java 8 Streams Lazy Evaluation: Click here for more details

  • Lazy evaluation means that a stream operation is not executed until it is absolutely necessary.
  • This can lead to significant performance gains when working with large data sets.
  • When you create a stream, it does not immediately process the data. Instead, it creates a “pipeline” of operations that will be performed on the data.
  • The pipeline is not executed until a terminal operation is called on the stream.Please click on above link for more details

Can you explain the concept of a Intermediate and Terminal operations in Java 8 streams?

Can you provide an example of using the filter method on a stream?

In this below example, people are huge data set and finds out adult males

List<Person> people =  new ArrayList<Person>();
Stream<Person> adults= people.stream()
                                .filter(p -> p.getAge() >= 18)
                                .filter(p -> p.getGender() == Gender.MALE);

Even Numbers

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Stream<Integer> evenNumbers = numbers.stream().filter(n -> n % 2 == 0);

can you provide example for map method to transform elements in a stream?

In this example, it will get the full name from the people’s list



List<Person> people =  new ArrayList<Person>();


Stream<String> fullName = people.stream()
                                .map(p -> p.getFirstName() + " " + p.getLastName());