- What is a Functional Interface? What is SAM Interface?
In Java, functional interfaces happen to be the new additions. It permits one abstract way inside them. The interfaces are referred to as the interfaces of Single Abstract Method or SAM Interfaces. These are represented by using the lambda expressions in Java. It is also used in the constructor references and even method reference and.
SAM interface is the interface that has one abstract method. It is also known as the Single Abstract Method Interfaces. By a single abstract method, it means either an abstract or a default method. And its implementation has been available by default. SAM interfaces’ instances happen to be java.lang.Runnable, java.util.Comparator, java.awt.event.ActionListener, as well as java.util.concurrent.Callable. These interfaces are implemented by using lambda expressions. But it can also work with the best use of the method references.
- Is it possible to define our Functional Interface? What is @FunctionalInterface? What are the rules to define a Functional Interface?
The functional interface happens to be the interface containing the only abstract method. So, that means they only have one functionality for exhibiting. When it comes to Java 8 (and onwards), the lambda expressions are used for representing an instance of the functional interface. However, before, there was a chance to create anonymous class objects or even implement the interfaces.
@FunctionalInterface annotation is used for ensuring that the functional interface wouldn’t have over the only abstract method. When more than one abstract method is implemented, then the compiler will flag the message concerning ‘Unexpected @FunctionalInterface annotation.’ Nevertheless, it is never mandatory to use such annotations.
The rules include:
- include just a single abstract method
- include any number of the default method
- declaring the abstract method
- Is @FunctionalInterface annotation mandatory to define a Functional Interface? What is the use of @FunctionalInterface annotation? Why do we need Functional Interfaces in Java?
The interface with one abstract method is known as the Functional Interface. The @FunctionalInterface annotation gets added so that one can mark the interface as the functional interface. However, it is never mandatory for use. Nevertheless, the practice of it to use with the functional interfaces is evident. It helps in avoiding the addition of other methods.
@FunctionalInterface annotation is only used for ensuring that functional interface doesn’t have more than one single abstract method. Its purpose is this only.
Functional interface happens to be an important concept when it comes to Java 8. It powers the lambda expression. However, a wide range of developers does not put effort into understanding it. They tend to spend their time in learning the lambda expression. But it is quite essential for Java 8.
- When do we go for Java 8 Stream API? Why do we need to use Java 8 Stream API in our projects?
Stream API in Java 8 had been introduced to process a collection of the objects. The stream happens to be the sequence of the objects that can support a wide range of methods pipelined for producing desired results. The features that have been included in Java are mentioned right below:
The intermediate operations are executed lazily. After that, it will return the stream. Therefore, multiple intermediate operations are pipelined here.
The streams don’t change those original structures. They just offer results as per the pipeline methods.
It’s never considered as a data structure. But it is the one taking the inputs from channel and collections.
Thus, using Java 8 Stream API is very important for the projects.
- Explain the Differences between Collection API and Stream API?
Streams and Collections are two different things when you think conceptually. These are used for different purposes. The main differences between the collection API and stream API have been mentioned below. To know more on this front, keep reading.
The Conceptual Difference – Storing and grouping data in a specific structure is possible in the collection. On the contrary, streams get used for performing complicated data process operations.
Data Modification – One can add or remove the elements from collection. However, for the stream, it only consumes the source and operates with a good result.
Iteration – Collections are meant to get iterated by an external process. On the other hand, Streams are meant to be iterated by an internal process.
- What is Spliterator in Java SE 8?Differences between Iterator and Spliterator in Java SE 8
Spliteratorare introduced in Java 8 programming. The best thing that it does is to offer support for some parallel processing of elements for collection. It even provides the method of try advance() for iterating the elements individually and that too in various threads. It also helps in parallel processing as well. Additionally, it helps combine next() and hasNext() operations to a single method.
The primary differences between Iterator and Spliteratorhave been mentioned below:
- Spliterator is used with Java 8’s Streams. Iterator is used with collection
- Spliterator make use of Internal Iteration, while Iterator makes use of External Iteration
- Spliteratoriterates the Streams in Sequential and Parallelmanner. Iterator will iterate in the Sequential manner
- What is Optional in Java 8? What is the use of Optional? Advantages of Java 8 Optional
Optional happens to be the container object that is used for containing the not-null objects. It helps represent null with the absent value. It comes with a wide range of utility methods for facilitating the code and handling the values. It has been introduced in Java 8 and happens to be similar to the Optional in Guava.
The advantages of Java 8 Optional have been mentioned below:
- It helps in eliminating the need for the null checks.
- One doesn’t require the NullPointerException at a specific run-time.
- It helps in developing a clean as well as neat APIs.
- The need for Boilerplate code is just excluded with Java 8 Optional
- It acts as the container for holding at one value.
- What is Type Inference? Is Type Inference available in older versions like Java 7 and before 7, or is it available only in Java SE 8?
Type inference happens to be an automatic deduction of types of data concerning some expressions in the language of programming. It is mainly usually performed at compiling time. It also involves the analysis of the program and inferring various types of expressions in the program.
The type inference also can often be the compiler feature included in functional programming. The interpreter or compiler needs only minimal information and context for figuring out the type of data of the variable! Java offers an improved version of it with 8.
The inference algorithm determines types of argument and then returns the value type. It again tries finding a specific kind of data that can work with arguments.