Hi Friends,
In previous post on java 8 interview questions : Java 8 - Part - I , I shared multiple questions and answers.
I'm extending that post here in Part - II.
You can also read my interview questions-answers in I.T. companies from these posts:
In previous post on java 8 interview questions : Java 8 - Part - I , I shared multiple questions and answers.
I'm extending that post here in Part - II.
You can also read my interview questions-answers in I.T. companies from these posts:
- Java Interview @ Aricent - Part - 2
- Java Interview @ Gemini Solutions
- Java Interview @ OLX
- Java Interview @ SAP
- Java Interview @ GalaxE
- Java Interview @ OrangeMantra
- Java Interview @ Concirrus
- Java Interview @ NexGen Solutions
- Java Interview @ Capgemini
- Java Interview @ Dew Solutions
Java 8 interview questions:
Question 1:
What are the benefits of using Streams?
Answer:
Streams were added in java 8 as part of java.util.stream package.
There are multiple benefits of using streams in java :
- Using streams, we can process data in declarative way as we do in SQL.
- Using parallel streams, we can use all available cores in the processor and process data parallely, which improves performance.
- Using streams, we can compose functions and data flows through the functions.
Question 2:
Can we use streams with primitives?
Answer:
Yes, there are two ways to use stream on primitives:
- Use wrapper classes.
- Use primitive types of streams: IntStream, LongStream and DoubleStream.
Question 3:
What is the difference between passing int array and string array to Stream.of()?
Answer:
Stream.of(int[]) gives Stream<int[]>.
Stream.of(String[]) gives Stream<String>
So, when using Stream.of() with int[] array , we get Stream<int[]> and then for getting ints from this stream, we use flatMapToInt(i-> Arrays.stream(i)) to get IntStream and then we can use either map() or forEach().
Question 4:
What is a boxed stream in java 8?
Answer:
If we want to convert stream of objects to collection, we can use:
List<String> list = Stream.of("a","b","c","d","e").collect(Collectors.toList());
The same process doesn't work on streams of primitives, however.
IntStream.of(1,2,3,4,5).collect(Collectors.toList()); // Compilation Error!!
To convert a stream of primitives, we must first box the elements in their wrapper class and then collect them. This type of stream is called boxed stream.
List<Integer> list = IntStream.of(1,2,3,4,5).boxed().collect(Collectors.toList());
System.out.println(list);
O/P: [1,2,3,4,5]
Question 5:
What is the difference between sequential stream and parallel stream?
Answer:
Parallel stream divides the task into many and run them in different threads, using multiple cores of the computer. It uses main thread along with ForkJoinPool.commonPool worker threads.
Sequential stream uses a single core. So, it uses only 1 thread that is main thread.
Question 6:
What are the characteristics of a stream?
Answer:
Below is the list of characteristics of streams in java 8:
- Sequence of elements: A stream provides a set of elements of specific types in a sequential manner. A stream computes elements on demand. It never stores the elements.
- Source: Stream takes Collections, Arrays or I/O resources as input source.
- Aggregate Operations: Stream supports aggregate operations like filter, map, limit, reduce, find, match and so on.
- Pipelining: Most of the stream operations return stream itself so that their result can be pipelined. These operations are called intermediate operations.
- Automatic Iterations: Stream operations do the iterations internally over the source elements provided, in contrast to collections where explicit iteration is required.
Question 7:
Draw Stream lifecycle diagram.
Answer:
Question 8:
What is architecture of Steam?
Answer:
Question 9:
How streams are lazy?
Answer:
Streams are lazy because intermediate operations are not evaluated unless terminal operation is invoked.
Each intermediate operation creates a new stream, stores the provided operation/function and return the new stream. The pipeline accumulates these newly created streams.
That's all for this post.
Thanks for reading !!