197.
198.
CHAPTER
01
Functional Interfaces
What is a functional interface? How can you create a
functional interface?
Answer:
A functional interface is an interface that has only one abstract
method. In order to
create a functional interface, you simply need
to create an interface that has just
one abstract method as shown
below:
@FunctionalInterface
public interface Multiplier {
public int multiply(int a, int b);
}
This code defines an interface called Multiplier. It has only a
multiply method. It has the @FunctionalInterface
annotation specified.
This annotation is optional, it marks the
interface as a functional interface. So,
if you try to add another
abstract method to the interface, the
code causes a compilation
error.
Explain the java.util.Function package.
Answer:
The java.util.function is a new package added by Java 8. It has a
lot of built–in functional interfaces. Some
of the interfaces in this
package are as follows: