Database Reference
In-Depth Information
We can either define our function classes inline as anonymous inner classes
(
Example 3-22
), or create a named class (
Example 3-23
).
Example 3-22. Java function passing with anonymous inner class
RDD
<
String
>
errors
=
lines
.
filter
(
new
Function
<
String
,
Boolean
>()
{
public
Boolean
call
(
String
x
)
{
return
x
.
contains
(
"error"
);
}
});
Example 3-23. Java function passing with named class
class
ContainsError
implements
Function
<
String
,
Boolean
>()
{
public
Boolean
call
(
String
x
)
{
return
x
.
contains
(
"error"
);
}
}
RDD
<
String
>
errors
=
lines
.
filter
(
new
ContainsError
());
The style to choose is a personal preference, but we find that top-level named func‐
tions are often cleaner for organizing large programs. One other benefit of top-level
functions is that you can give them constructor parameters, as shown in
Example 3-24
.
Example 3-24. Java function class with parameters
class
Contains
implements
Function
<
String
,
Boolean
>()
{
private
String
query
;
public
Contains
(
String
query
)
{
this
.
query
=
query
;
}
public
Boolean
call
(
String
x
)
{
return
x
.
contains
(
query
);
}
}
RDD
<
String
>
errors
=
lines
.
filter
(
new
Contains
(
"error"
));
In Java 8, you can also use lambda expressions to concisely implement the function
interfaces. Since Java 8 is still relatively new as of this writing, our examples use the
more verbose syntax for defining classes in previous versions of Java. However, with
lambda expressions, our search example would look like
Example 3-25
.
Example 3-25. Java function passing with lambda expression in Java 8
RDD
<
String
>
errors
=
lines
.
filter
(
s
->
s
.
contains
(
"error"
));
If you are interested in using Java 8's lambda expression, refer to
Oracle's documen‐