Fichas
Java 8
Fichas del conjunto (48)
- 1In Java 8 how can I filter a collection using the stream?public static void main(String[] args)
{
// Creating a list of Integers
List list = Arrays.asList(3, 4, 6, 12, 20);
// Getting a stream consisting of the
// elements that are divisible by 5
// Using Stream filter(Predicate predicate)
list.stream()
.filter(num -> num % 5 == 0)
.forEach(System.out::println);
}
Output
20
Explanation
It provides a method filter () to filter stream elements on the basis of given predicate.
This method take predicate as an argument and returns a stream of consisting of resulted elements. - 2Can we define default method without using default keyword in Java8?No, for defining default method inside interface default keyword is must and it should prefix method declaration.
Without prefixing default keyword results in compilation error.
Compile-time error: Abstract methods do not specify a body.
Reason: without default keyword, compiler consider it as abstract method which doesn’t have body. - 3What actual advantage does Java 8 brings?Code is more concise and readable.
More reusable,testable and maintable.
User can write parallel code.
User can write database like operations. - 4Explain us about Functional interfaces.An Interface that contains exactly one abstract method is known as functional interface. It can have any number of defaults, static methods but can contain only one abstract method. It can also declare methods of object class. Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces.
- 5How lambda expressions and functional interface are related?Lambda expressions express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable).
Lambda expressions implement the only abstract function and therefore implement functional interfaces - 6What is your understanding about stream pipelining?It is the process of chaining different operations together. It accomplishes this function by dividing stream operations into two categories, intermediate operation, and terminal operations.
- 7Can you create your own functional interface?Yes, we can create our own functional interface.
We must do these two things:
Annotate the interface with @FunctionalInterface, which is the Java 8 convention for custom functional interfaces.
Ensure that the interface has just one abstract method. - 8What will happen if we define multiple abstract methods inside the Functional interface?Inside Functional Interface we can take only one abstract method, if we take more than one abstract method then compiler raise an error. Interface can declares an abstract method overriding one of the public method from java.lang.Object, that still can be considered as functional interface.
- 9Why default methods needed in the interface?Java 8 has introduced the concept of default methods which allow the interfaces to have methods with implementation without affecting the classes that implement the interface.
- 10What is the behavior of findFirst() method in Java 8 streams?Stream findFirst() returns an Optional (a container object which may or may not contain a non-null value) describing the first element of this stream, or an empty Optional if the stream is empty.
Explanation
import java.util.stream.Stream;
public class Main
{
public static void main(String[] args)
{
//sequential stream
Stream.of("one", "two", "three", "four")
.findFirst()
.ifPresent(System.out::println);
//parallel stream
Stream.of("one", "two", "three", "four")
.parallel()
.findFirst()
.ifPresent(System.out::println);
}
}
Output
one
one - 11What is the behavior of findAny() method in Java 8 streams?Stream findAny() returns an Optional (a container object which may or may not contain a non-null value) describing some element of the stream, or an empty Optional if the stream is empty.
- 12When to use findAny() method in Java 8 streams?There are instances where you have a stream, but you only want to select a random element as long as it meets certain conditions and the operation itself takes the shortest time possible.
- 13What do you mean by stream?Streams are just sequence of data from a source.
With Java 8, we can do manipulation on data using stream API.
Example: Youtube
If we are watching a video all the content of the video will not be loaded at once. As the time elapses, we will get the content. - 14Can you tell me difference between Collection API and Stream API?Collection API is used for storing data in different kinds of data structures.
Stream API is used for computation of data on a large set of objects.
With Collection API we can store a finite number of elements in a data structure.
With Stream API, we can handle streams of data that can contain infinite number of elements.
Collection API constructs objects in an eager manner.
Stream API creates objects in a lazy manner.
Multiple consumption: Most of the Collection APIs support iteration and consumption of elements multiple times.
With Stream API we can consume or iterate elements only once. - 15Do you know what are the various forms of writing lambda expressions?We can declare a lambda expression by-
() -> expression
(parameters) -> expression
(parameters) -> { multiple statements} - 16What do you understand by @Functional Interface annotation in Java 8?@Functional interface - used to force the compiler to check whether the given interface has single-abstract method or not.
If not, compiler will throw error "Unexpected @FunctionalInterface annotation" - 17How do you create a custom annotation in Java?import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface TestAnnotationForClass {
String author();
String date();
int currentRevision() default 1;
String lastModified() default "Not applicable";
String lastModifiedBy() default "Not applicable";
}
@TestAnnotationForClass (
author = "Editorial Team",
date = "9/18/2020",
currentRevision = 6,
lastModified = "9/18/2020",
lastModifiedBy = "Editorial Team"
}
Explanation
In this example, we have used RetentionPolicy.RUNTIME because we want to demonstrate and read annotation at runtime. RetentionPolicy.RUNTIME indicates that annotations are to be recorded in the class file by the compiler and retained by the virtual machine at run time, so they may be read reflectively.
@TestAnnotationForClass(currentRevision=6, lastModified=9/18/2020, lastModifiedBy=Editorial Team, author=Editorial Team, date=9/18/2020) - 18How to avoid NullPointer exception in Java 8?Java 8 provides concept of Optional
Optional can be used to avoid NullPointer Exception
Example:
```Java
String value=null;
Optional value=Optional.empty(); - 19What are the different types of functional interfaces in Java 8?Consumer
Predicate
Function
Supplier - 20How would you convert object of type Iterable to Stream?import java.util.*;
import java.util.stream.*;
class Main{
// Function to get the Stream
public static Stream
getStreamFromIterable(Iterable iterable)
{
// Convert the Iterable to Spliterator
Spliterator
spliterator = iterable.spliterator();
// Get a Sequential Stream from spliterator
return StreamSupport.stream(spliterator, false);
}
public static void main(String[] args)
{
// Get the Iterator
Iterable
iterable = Arrays.asList(10,20,30,40,50);
// Get the Stream from the Iterable
Stream
stream = getStreamFromIterable(iterable);
// Print the elements of stream
stream.forEach(s -> System.out.println(s));
}
}
// Output:
// 10
// 20
// 30
// 40
// 50 - 21How to overcome, multiple inheritance problem in Java 8?we can do that by overriding the default method in the implementation class. Altogether provide new implementation or invoke either one of the default() method using super keyword For example, \.super.\
- 22What happens, if a class implements two interfaces having exactly same method with same signature (consider one as default and another abstract)?Compilation fails with error saying conflicting method.
Compile-time error: The default method displayDefaultMethod() inherited from DemoInterfaceA conflicts with another method inherited from DemoInterfaceB
To overcome this error, override this conflicting method and provide new implementation or invoke default method’s implementation using super keyword - 23What is the purpose of joining () method introduced in Java 8 ?The joining () method of the Collectors class in Java 8 returns a Collector that concatenates the input elements into a String.
public class Example {
public static void main (String[] args) {
List list = Arrays.asList('D', 'e', 'm', 'o');
String str = list. stream().map(String::valueOf).collect(Collectors.joining());
System.out.println("Concatenated = "+str);
}
}
Output
Concatenated = Demo - 24What is the use of the String::Value of expression in Java 8?String::ValueOf is a simple static method referencing the valueOf method, belonging to the class String.
- 25What do you mean by method reference in Java 8?It refers to methods of functional interfaces. It can be considered as a short-code version of using a lambda expression.
The following is the expression for a method reference:
`Class::methodname` - 26What is the easiest way to print the current date and time using the new APIs in Java 8?The now method, which is a part of LocalDate, can be used to get the current date as shown below:
```Java
LocalDate currentDate = LocalDate.now();
System.out.println(currentDate);
```
Similarly, it can also be used to get the current time:
```Java
LocalTime currentTime = LocalTime.now();
System.out.println(currentTime); - 27Differentiate between intermediate and terminal operations in Java 8.Intermediate Operation
Used for the transition to a new state.
Lazy execution of code, i.e., code is not executed as soon as it is encountered Not lazy.
Terminal Operation
Used to end the process under execution.
Code is immediately executed upon encounter. - 28Can the following piece of code compile successfully? @FunctionalInterface public interface Function2 { public V apply(T t, U u); default void count() { // increment counter } }Yes, the code can compile and execute without any errors. It uses functional interface specifications when the single abstract method is being defined.
- 29What is the code to sort strings using the Java 8 lambda expression?The below piece of code sorts of strings using the lambda expression:
//Sorting using Java 8 lambda expression
private void sortUsingJava8(List names) {
Collections.sort(names, (s1, s2) -> s1.compareTo(s2));
} - 30Is it possible to call a static method of any interface in a class using Java 8?Yes, it is possible to call a static method in a class by making use of the name as shown below:
interface Student {
static void Present() {
System.out.println("Student is there!");
}
}
class Associates implements Student {
public void print() {
Student.Present();
}
} - 31Do you know how the random keyword in Java 8 works?The random keyword, as the name suggests, is used to generate random values for computations and operations in Java 8.
The following piece of code is used to print out 20 random numbers using the for each loop:
Random random = new Random();
random.ints().limit(20).forEach(System.out::println); - 32Explain about collectors in Java 8?Collectors are mainly used to combine the result after the processing of elements in a stream. They are used to return lists or strings.
- 33What is the easiest way to print the sum of all the numbers present in a list using Java 8?In Java 8, the following code is used to print the sum of all of the numbers that are present in a list:
List numbers = Arrays.asList(5, 4, 10, 12, 87, 33, 75);
IntSummaryStatistics stats = integers.stream().mapToInt((x) −> x).summaryStatistics();
System.out.println("Sum of all numbers : " + stats.getSum()); - 34When is an ideal situation to use the Stream API in Java 8?The Stream API in Java 8 can be effectively used if the Java project calls for the following operations:
- Perform database operations
- Execute operations lazily
- Write functional-style programming
- Perform parallel processing
- Use pipeline operations
- Use internal iteration - 35Can you tell me about supplier in Java 8?A supplier is a simple functional interface in Java 8 that does not take in any argument. It is used as an assignment target when making use of lambda expressions.
- 36Do you know about predicate in Java 8 ?The Predicate is also a functional interface in Java and it is defined in the java.util.function package.
It improves the readability and manageability of our code. Also, it helps us in unit testing them separately.
We can use Predicate anywhere where we need to evaluate a condition on a Collection of objects such that evaluation can result either in true or false. - 37Can you name the common types of functional interfaces in the standard library?There are many functional interface types in the standard library, and some of them are as follows:
BiFuction
BinaryOperator
Consumer
Predicate
Supplier
UnaryOperator - 38Can you give examples of intermediate operations in Java 8?The examples that are widely used in intermediate operations are:
Distinct()
Limit(long n)
Filter(Predicate)
Map(Function)
skip(long n) - 39What are the similarities between map and flatMap stream operations in Java 8?Both map and flatMap operations are a form of intermediate stream operations that take in a function and use the input function for performing various activities in the stream.
- 40How to find and remove duplicate elements from a list using Java 8?Duplicate elements can be listed and removed easily by applying stream operations and performing a collection, later using the Collections.toSet() method. This should remove all the duplicate elements present in the list.
- 41What is the easiest way to convert an array into a stream in Java 8?Any array in Java 8 can be converted into a stream easily using the stream class. The creation of a stream using a factory method is as shown below:
String[] testarray = {"Hello", "Intellipaat", "learners"};
Stream numbers = Stream.of(testarray);
numbers.forEach(System.out::println); - 42What is the use of the peek() method in Java 8?The peek() method is a part of the stream class in Java 8, which is used to see actions performed through a stream pipeline. Peeking can be done at every step to print messages about the code being executed onto the console.
Peeking has a wide amount of usage when efficiency is a requirement, when debugging code with the lambda expression, or when performing stream processing. - 43What is the meaning of a Spliterator in Java 8?Spliterator is a newly introduced iterator interface for Java 8. It is very efficient and handles API-related operations seamlessly across the runtime environment.
- 44Explain the different time and date APIs and when you may use them.Important classes of Date-Time API are:
Local: It is used to simplify the date-time API with no complexity of timezone handling.
Zonal: It is a specialized date-time API to deal with various timezones.
Java LocalDate Time Class:
It is an immutable date-time object that represents a date-time, with the default format as yyyy-MM-dd-HH-mm-ss.zzz. It is used when time zones are NOT required.
ZonedDate Time Class in Java:
It is an immutable representation of a date-time with a time-zone. Use it when time zones are to be considered. It is used to store all date and time fields. - 45In Java, how do you convert a String to a LocalDate or a LocalDateTime? ?The Java 8 `LocalDate-Time API` includes a `parse()` method, which can be used to parse a given input string using a specified format.
For example,
LocalDate newDate = LocalDate.parse("2016-08-23");
System.out.println("Parsed date : " + newDate); - 46What is the difference between a predicate and a function?A predicate takes one argument and returns a boolean value.
A function takes one argument and returns an object.
Both are useful for evaluating lambda expressions. - 47Can a functional interface extend/inherit another interface?A functional interface cannot extend another interface with abstract methods as it will void the rule of one abstract method per functional interface.
Example:
interface Parent {
public int parentMethod();
}
@FunctionalInterface // This cannot be FunctionalInterface
interface Child extends Parent {
public int childMethod();
// It will also extend the abstract method of the Parent Interface
// Hence it will have more than one abstract method
// And will give a compiler error
}
It can extend other interfaces which do not have any abstract method and only have the default, static, another class is overridden, and normal methods. - 48What are the advantages of using the Optional class?It encapsulates optional values, i.e., null or not-null values, which helps in avoiding null checks, which results in better, readable, and robust code It acts as a wrapper around the object and returns an object instead of a value, which can be used to avoid run-time NullPointerExceptions.