Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Java Streaming API Tutorial

Question 1:

What is Java Streaming API and how is it different from collections?

Answer:

The Java Streaming API is introduced in Java 8 to process collections of data in a functional programming style. It provides a concise and expressive way to perform operations on collections. Unlike collections, streams do not store data; they operate on the source data and produce a result without modifying the underlying data.

Code Example:


List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().mapToInt(Integer::intValue).sum();
  

Question 2:

Explain the difference between intermediate and terminal operations in Java Streaming API.

Answer:

In Java Streaming API, intermediate operations are operations that transform a stream into another stream. They are lazy and do not execute until a terminal operation is invoked. Terminal operations, on the other hand, produce a result or a side-effect, and they trigger the execution of the entire stream pipeline.

Code Example:


List<String> words = Arrays.asList("Hello", "World");
long count = words.stream().filter(s -> s.length() > 3).count();
  

Question 3:

What is lazy evaluation in the context of Java Streaming API?

Answer:

Lazy evaluation in Java Streaming API means that the operations are not executed until a result is actually needed. Intermediate operations are only performed when a terminal operation is invoked. This allows for more efficient processing of large datasets.

Code Example:


List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> squaredNumbers = numbers.stream().map(x -> x * x);

Question 4:

Explain the difference between forEach and forEachOrdered in Java Streaming API.

Answer:

Both forEach and forEachOrdered are terminal operations used to iterate over elements of a stream. However, forEach does not guarantee any specific order, while forEachOrdered processes the elements in the order specified by the stream's encounter order, if any.

Code Example:


List<String> words = Arrays.asList("apple", "banana", "cherry");
words.stream().forEach(System.out::println);
words.stream().forEachOrdered(System.out::println);

Question 5:

What is the purpose of the distinct() operation in Java Streaming API?

Answer:

The distinct() operation is used to eliminate duplicates from a stream. It returns a stream consisting of the distinct elements of the original stream. The distinctness is determined by the equals() method of the elements.

Code Example:


List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5);
List<Integer> distinctNumbers = numbers.stream().distinct().collect(Collectors.toList());

Question 6:

What is the purpose of the reduce() operation in Java Streaming API?

Answer:

The reduce() operation in Java Streaming API is used to combine the elements of a stream into a single result. It takes an associative accumulation function and returns an optional value.

Code Example:


List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Optional<Integer> sum = numbers.stream().reduce(Integer::sum);

Question 7:

What is the difference between map() and flatMap() in Java Streaming API?

Answer:

The map() operation is used to transform each element of a stream using a provided function, while flatMap() is used to flatten the elements after applying a function. flatMap() is useful when the transformation produces a stream of values for each element.

Code Example:


List<String> words = Arrays.asList("Hello", "World");
List<String> distinctLetters = words.stream()
  .map(s -> s.split(""))
  .flatMap(Arrays::stream)
  .distinct()
  .collect(Collectors.toList());

Question 8:

What is the purpose of the peek() operation in Java Streaming API?

Answer:

The peek() operation is used for debugging and logging. It allows you to perform an action on each element of the stream without modifying the elements. It is often used for logging or printing intermediate values during the stream pipeline.

Code Example:


List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> squaredNumbers = numbers.stream()
 .map(x -> x * x)
 .peek(System.out::println)
 .collect(Collectors.toList());

Question 9:

How does the filter() operation work in Java Streaming API?

Answer:

The filter() operation is used to select elements from a stream based on a given predicate. It returns a new stream that includes only the elements that satisfy the specified condition.

Code Example:


List<String> fruits = Arrays.asList("apple", "banana", "cherry");
List<String> filteredFruits = fruits.stream()
  .filter(s -> s.startsWith("b"))
  .collect(Collectors.toList());

Question 10:

Explain the purpose of the collect() operation in Java Streaming API.

Answer:

The collect() operation is a terminal operation in Java Streaming API used to transform the elements of a stream into a different form, usually a collection like a List, Set, or Map. It takes a Collector as an argument, which specifies how the elements should be accumulated.

Code Example:


List<String> words = Arrays.asList("Java", "is", "powerful");
String result = words.stream().collect(Collectors.joining(" "));

Question 11:

What is the difference between findFirst() and findAny() in Java Streaming API?

Answer:

The findFirst() operation returns the first element of the stream, whereas findAny() returns any element of the stream. For parallel streams, findAny() is more suitable as it can provide better performance by allowing the runtime to choose any available element.

Code Example:


List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Optional<Integer> firstElement = numbers.stream().findFirst();
Optional<Integer> anyElement = numbers.parallelStream().findAny();

Question 12:

What is the purpose of the allMatch() operation in Java Streaming API?

Answer:

The allMatch() operation checks whether all elements of a stream match a given predicate. It returns true if all elements satisfy the condition; otherwise, it returns false.

Code Example:


List<String> words = Arrays.asList("apple", "banana", "cherry");
boolean allStartWithA = words.stream().allMatch(s -> s.startsWith("a"));

Question 13:

How can you create an infinite stream using Java Streaming API?

Answer:

An infinite stream can be created using the Stream.iterate() or Stream.generate() methods. The iterate() method takes an initial seed and a UnaryOperator to generate the next element, while generate() takes a Supplier to produce elements.

Code Example:


Stream<Integer> infiniteStream = Stream.iterate(0, n -> n + 1);

Question 14:

What is the purpose of the min() and max() operations in Java Streaming API?

Answer:

The min() operation returns the minimum element of a stream based on a provided comparator, while the max() operation returns the maximum element. These operations are terminal and return an Optional to handle the possibility of an empty stream.

Code Example:


List<Integer> numbers = Arrays.asList(4, 2, 7, 1, 9);
Optional<Integer> minNumber = numbers.stream().min(Integer::compare);
Optional<Integer> maxNumber = numbers.stream().max(Integer::compare);

Question 15:

Explain the concept of parallel streams in Java Streaming API.

Answer:

Parallel streams in Java Streaming API allow for concurrent processing of elements. They are created using the parallel() method on a stream. Operations on parallel streams are divided into multiple tasks and executed concurrently, potentially improving performance for large datasets.

Code Example:


List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sumParallel = numbers.parallelStream().mapToInt(Integer::intValue).sum();

Question 16:

What is the difference between a sequential stream and a parallel stream in Java Streaming API?

Answer:

A sequential stream processes elements in a single-threaded fashion, while a parallel stream processes elements concurrently using multiple threads. Parallel streams are designed to take advantage of multi-core processors, providing potential performance improvements for computationally intensive operations.

Code Example:


List<String> words = Arrays.asList("apple", "banana", "cherry");
long countSequential = words.stream().filter(s -> s.length() > 5).count();
long countParallel = words.parallelStream().filter(s -> s.length() > 5).count();

Question 17:

Explain the purpose of the groupingBy() collector in Java Streaming API.

Answer:

The groupingBy() collector is used to group elements of a stream based on a classifier function. It returns a Map where the keys are the result of applying the classifier function, and the values are Lists of items that share the same key.

Code Example:


List<String> words = Arrays.asList("apple", "banana", "cherry", "date");
Map<Integer, List<String>> groupedByLength = words.stream()
   .collect(Collectors.groupingBy(String::length));

Question 18:

How does the forEachOrdered() operation differ from forEach() when applied to parallel streams?

Answer:

When applied to parallel streams, forEachOrdered() guarantees that the order of elements is maintained during processing, similar to sequential streams. On the other hand, forEach() may process elements in any order, potentially leading to different results when the stream is parallelized.

Code Example:


List<String> words = Arrays.asList("apple", "banana", "cherry");
words.parallelStream().forEachOrdered(System.out::println);

Question 19:

Explain the purpose of the toArray() method in Java Streaming API.

Answer:

The toArray() method in Java Streaming API is used to convert the elements of a stream into an array. It returns an array containing the elements of the stream in encounter order. The type of the resulting array is determined by the provided generator function or the array's runtime type.

Code Example:


List<String> words = Arrays.asList("apple", "banana", "cherry");
String[] wordsArray = words.stream().toArray(String[]::new);

Question 20:

Explain the purpose of the partitioningBy() collector in Java Streaming API.

Answer:

The partitioningBy() collector is used to partition elements of a stream into two groups based on a predicate. It returns a Map with Boolean keys (true and false), where the elements that satisfy the predicate are in the "true" group, and the rest are in the "false" group.

Code Example:


List<String> words = Arrays.asList("apple", "banana", "cherry", "date");
Map<Boolean, List<String>> partitioned = words.stream()
 .collect(Collectors.partitioningBy(s -> s.length() > 5));
©2024 WithoutBook