Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you have a list of Java 8 Functional interfaces (not the ones listed in java.util.function)?

I'm trying to see if there is any way to get a list of all the interfaces in Java 8 that are functional interfaces. I'm not talking about the list on this page:

https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

Rather, I'm talking about interfaces like Comparator, FileFilter, and Runnable - interfaces that the API document shows are functional like this:

@FunctionalInterface public interface Runnable

Is there a full list of these somewhere?

Thank you!

like image 389
Elisabeth Avatar asked Mar 22 '17 03:03

Elisabeth


People also ask

How many functional interfaces does Java 8 have?

In Java 8, there are 4 main functional interfaces are introduced which could be used in different scenarios.

Which of the following functional interfaces are currently introduced in Java 8?

Which of the following functional interfaces were currently introduced in Java 8: Runnable. List. Callable.


2 Answers

There is a list of all interfaces being annotated with @FunctionalInterface available in the API documentation, when you browse to the FunctionalInterface’s class documentation and click on the USE link at the top.

But it must be emphasized that the presence of the annotation is not mandatory to make an interface a functional interface. Each interface having exactly one abstract method not matching a public method of java.lang.Object can be implemented via lambda expressions or method references, though that doesn’t necessarily implies that the result will fulfill the additional contracts specified for the particular interface.

There are roughly 200 interfaces in the JRE fulfilling the technical constraints, so the compiler wouldn’t object when you try to implement them via lambda expression. Only a few of them have the annotation. Some of those not having the annotation will still work smoothly, e.g. ActionListener, InvocationHandler, or ThreadFactory, whereas others are unsuitable due to additional constraints like Comparable, ProtocolFamily, FlavorException. This is also discussed in “Why isn't @FunctionalInterface used on all the interfaces in the JDK that qualify?”

So while @FunctionalInterface documents the intention of being usable as target type of a lambda expression or method reference, other interface types may still be suitable for the same purpose, but you have to investigate the contracts yourself to conclude whether the use is appropriate.

like image 182
Holger Avatar answered Sep 23 '22 17:09

Holger


Using @GhostCat's Eclipse method, here's the actual list of interfaces marked as @FunctionalInterface in the runtime library, excluding java.util.function.*:

java.awt.KeyEventDispatcher
java.awt.KeyEventPostProcessor
java.io.FileFilter
java.io.FilnameFilter
java.lang.Runnable
java.lang.Thread.UncaughtExceptionHandler
java.nio.file.DirectoryStream.Filter
java.nio.file.PathMatcher
java.time.temporal.TemporalAdjuster
java.time.temporal.TemporalQuery
java.util.Comparator
java.util.concurrent.Callable
java.util.logging.Filter
java.util.prefs.PreferenceChangeListener
jdk.management.resource.ResourceApprover
jdk.management.resource.ResourceId
like image 37
shmosel Avatar answered Sep 21 '22 17:09

shmosel