Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Java 8 Interview Questions and Answers

Test your skills through the online practice test: Java 8 Quiz Online Practice Test

Related differences

Java 7 vs Java 8Java 8 vs Java 9

Ques 26. What Is a Stream? How Does It Differ from a Collection?

  • In simple terms, a stream is an iterator whose role is to accept a set of actions to apply on each of the elements it contains.
  • The stream represents a sequence of objects from a source such as a collection, which supports aggregate operations. They were designed to make collection processing simple and concise. Contrary to the collections, the logic of iteration is implemented inside the stream, so we can use methods like map and flatMap for performing a declarative processing.
  • And yet another important distinction from collections is that streams are inherently lazily loaded and processed.
  • Another difference is that the Stream API is fluent and allows pipelining:  
    • int sum = Arrays.stream(new int[]{1, 2, 3})
        .filter(i -> i >= 2)
        .map(i -> i * 3)
        .sum();

Is it helpful? Add Comment View Comments
 

Ques 27. What Is the Difference Between Map and flatMap Stream Operation?

  • There is a difference in signature between map and flatMap. Generally speaking, a map operation wraps its return value inside its ordinal type while flatMap does not.
  • For example, in Optional, a map operation would return Optional<String> type while flatMap would return String type.
  • So after mapping, one needs to unwrap (read “flatten”) the object to retrieve the value whereas, after flat mapping, there is no such need as the object is already flattened. The same concept is applied to mapping and flat mapping in Stream.
  • Both map and flatMap are intermediate stream operations that receive a function and apply this function to all elements of a stream.
  • The difference is that for the map, this function returns a value, but for flatMap, this function returns a stream. The flatMap operation “flattens” the streams into one.

Is it helpful? Add Comment View Comments
 

Ques 28. What is Stream Pipelining in Java 8?

  • Stream pipelining is the concept of chaining operations together. This is done by splitting the operations that can happen on a stream into two categories: intermediate operations and terminal operations.
  • Each intermediate operation returns an instance of Stream itself when it runs, an arbitrary number of intermediate operations can, therefore, be set up to process data forming a processing pipeline.
  • There must then be a terminal operation which returns a final value and terminates the pipeline.

Is it helpful? Add Comment View Comments
 

Ques 29. What does the flatmap() function do? why you need it?

The flatmap function is an extension of the map function. Apart from transforming one object into another, it can also flatten it.
For example, if you have a list of the list but you want to combine all elements of lists into just one list. In this case, you can use flatMap() for flattening. At the same time, you can also transform an object like you do use map() function.

public static void main(String[] args) {
            String[][] data = new String[][]{{"a", "b"}, {"c", "d"}, {"e", "f"}};
            //Stream<String[]>
            Stream<String[]> temp = Arrays.stream(data);
            //Stream<String>, GOOD!
            Stream<String> stringStream = temp.flatMap(x -> Arrays.stream(x));
            Stream<String> stream = stringStream.filter(x -> "a".equals(x.toString()));
            stream.forEach(System.out::println);
       }

Is it helpful? Add Comment View Comments
 

Ques 30. What is the parallel Stream? How can you get a parallel stream from a List?

A parallel stream can parallel execute stream processing tasks. For example, if you have a parallel stream of 1 million orders and you are looking for orders worth more than 1 million, then you can use a filter to do that.
Unlike sequential Stream, the parallel Stream can launch multiple threads to search for those orders on the different part of Stream and then combine the result.
In short, the parallel Stream can paralyze execution.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook