Java Reference
In-Depth Information
Creating Your Own Functional Interfaces
Problem
You want to create functional interfaces for use within lambda expressions.
Solution
Create an interface with one abstract method; optionally annotate it with @FunctionalIn-
terface .
Discussion
As mentioned previously, a functional interface is one with a single abstract method. Some
well-known functional interfaces are java.lang.Runnable , java.util.Observer , and
java.awt.event.ActionListener . Some “nonfunctional” (in this sense) interfaces include
java.util.Observable and java.awt.event.WindowListener , each of which has more
than one method.
You can create your own functional interface easily. However, before you do so, consider
that a large selection of these is already available in the JDK! As discussed in Using Lambda
Predefined Interfaces Instead of Your Own , you should check the documentation for the
package java.util.function , which provides lots of predefined general-purpose function-
al interfaces, including Predicate , which we've already used.
So, you still want to do it yourself? A simple example of a functional interface would be:
Search WWH ::




Custom Search