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


Download 1.52 Mb.
Pdf ko'rish
bet77/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   73   74   75   76   77   78   79   80   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Java 8?
Answer:
The primitive specializations of the in–built functional interfaces
like IntSupplier, BooleanSupplier, etc. are added by Java
to improve performance. The primitive specializations do away
with the need for autoboxing and hence improve performance.
The following code demonstrates this:
Supplier integerSupplier = ()–> new Integer(new
Random().nextInt());
This code creates a Supplier that returns a random integer less
than 100. Since the primitive specialization is not used, the value
returned by Random class needs to be wrapped as an Integer.
Now consider the following code:
IntSupplier integerSupplier2 = ()–> new
Random().nextInt();
In this case an IntSupplier which is the primitive
specialization of Supplier that returns an integer is used. So,
there is no need to wrap the return value as an Integer. Since
autoboxing is avoided, this code results in better performance.


206. Where are the built–in functional interfaces commonly
used?
Answer:
Java 8 has added the Stream API that helps to perform bulk
operations on Collections. The built-in functional interfaces are
commonly used as parameters to the methods in the stream API.
The Stream methods apply the operation specified by the
functional interface to each element in the Stream. For example,
the Stream interface has a filter method that filters a Collection
based on some condition. The condition is specified via the
Predicate interface which is an in-built functional interface. It
checks if each value in the input Stream matches the condition
specified by the Predicate. Similarly, the Stream interface has a
method called map that transforms the elements in a Stream. It
accepts as parameter a Function instance which is an in-built
functional interface. It applies the operation specified by the
Function interface to each element in the Stream.




Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   73   74   75   76   77   78   79   80   ...   171




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