Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet105/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   101   102   103   104   105   106   107   108   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

268.
a.
b.
c.
269.
CHAPTER
02
Stream/Collection
Improvements
What are some of the improvements made by Java 9 on
the Stream Interface?
Answer:
The following are some of the improvements made by Java 9 on
the Stream interface:
Stream.ofNullable: Java 9 has added a
Stream.ofNullable() method on the Stream interface.
This helps to create a Stream with a value that may be null.
So, if a non–null value is passes to this method, it creates a
Stream with that method, otherwise it creates an empty
stream.
takeWhile, dropWhile: Both the takeWhile() and
dropWhile() methods operate on a Stream. They can be
used to obtain a subset of the input Stream.
Stream.iterate: Java 9 has added an overloaded version
of the Stream.iterate() method that accepts a
Predicate and terminates the Stream when the condition
specified by the Predicate is true.
Is the code below valid? If not, what can be done to fix it?
Stream strStream = Stream.of(null);
strStream.forEach(str–> System.out.println(str));


270.
Answer:
The code above is valid and does not cause any compilation
error. However, when it is executed, it will cause a
NullPointerException. This is because you cannot create a
Stream with a null value using the Stream.of() method. In
order to fix the code above, you need to make the following
change:
Stream strStream = Stream.ofNullable(null);
strStream.forEach(str–> System.out.println(str));
Java 9 had added the ofNullable() method to the Stream
interface that helps you create a Stream with a null value. So,
the code above does not cause a NullPointerException.
Identify the issue in the code snippet below and explain

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   101   102   103   104   105   106   107   108   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling