Java Reference
In-Depth Information
In Java, you can convert an object to a stream of bytes and restore the object back later. This is called
serialization
.
A class must implement the
java.io.Serializable
marker interface for its objects to be serialized. If you want a
lambda expression to be serialized, you will need to use a cast with an intersection type. The following statement
assigns a lambda expression to a variable of the
Serializable
interface:
Serializable ser = (Serializable & Adder) (x, y) -> x + y;
■
Tip
i will cover the
Serializable
interface and serialization of objects in Chapter 7.
Commonly Used Functional Interfaces
Java 8 has added many frequently used functional interfaces in the package
java.util.function
. They are
listed in Table
5-2
.
Table 5-2.
List of Functional Interfaces Declared in the Package java.util.function
Interface Name
Method
Description
Function<T,R>
R apply(T t)
Represents a function that takes an argument of type
T
and returns a result of type
R
.
BiFunction<T,U,R>
R apply(T t, U u)
Represents a function that takes two arguments of types
T
and
U
, and returns a result of type
R
.
Predicate<T>
boolean test(T t)
In mathematics, a predicate is a boolean-valued function
that takes an argument and returns true or false. The
function represents a condition that returns true or false
for the specified argument.
BiPredicate<T,U>
boolean test(T t, U u)
Represents a predicate with two arguments.
Consumer<T>
void accept(T t)
Represents an operation that takes an argument, operates
on it to produce some side effects, and returns no result.
BiConsumer<T,U>
void accept(T t, U u)
Represents an operation that takes two arguments,
operates on them to produce some side effects, and
returns no result.
Supplier<T>
T get()
Represents a supplier that returns a value.
UnaryOperator<T>
T apply(T t)
Inherits from
Function<T,T>. R
epresents a function that
takes an argument and returns a result of the same type.
BinaryOperator<T>
T apply(T t1, T t2)
Inherits from
BiFunction<T,T,T>
. Represents a function
that takes two arguments of the same type and returns a
result of the same.
Table
5-2
shows only the generic versions of the functional interfaces. Several specialized versions of these
interfaces exist. They have been specialized for frequently used primitive data types; for example,
IntConsumer
is a
specialized version of
Consumer<T>
. Some interfaces in the table contain convenience default and static methods.
The table lists only the abstract method, not the default and static methods.