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

5

19

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!

Jugate answered 22/3, 2017 at 3:43 Comment(0)
D
20

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.

Dampproof answered 22/3, 2017 at 18:31 Comment(0)
D
7

For completeness, here is the list of all JRE interfaces that can be implemented via lambda expression or method reference, though not all of them would be useful or semantically correct when being implemented that way. This list doesn’t include extended APIs like JavaFX.

java.awt.ActiveEvent
java.awt.Composite
java.awt.KeyEventDispatcher
java.awt.KeyEventPostProcessor
java.awt.PrintGraphics
java.awt.Stroke
java.awt.Transparency
java.awt.datatransfer.ClipboardOwner
java.awt.datatransfer.FlavorListener
java.awt.dnd.DragGestureListener
java.awt.dnd.DragSourceMotionListener
java.awt.event.AWTEventListener
java.awt.event.ActionListener
java.awt.event.AdjustmentListener
java.awt.event.HierarchyListener
java.awt.event.ItemListener
java.awt.event.MouseWheelListener
java.awt.event.TextListener
java.awt.event.WindowStateListener
java.awt.image.ImageObserver
java.awt.image.TileObserver
java.awt.image.renderable.RenderedImageFactory
java.awt.print.Printable
java.awt.print.PrinterGraphics
java.beans.ExceptionListener
java.beans.PropertyChangeListener
java.beans.VetoableChangeListener
java.beans.beancontext.BeanContextChildComponentProxy
java.beans.beancontext.BeanContextContainerProxy
java.beans.beancontext.BeanContextProxy
java.beans.beancontext.BeanContextServiceRevokedListener
java.io.Closeable
java.io.FileFilter
java.io.FilenameFilter
java.io.Flushable
java.io.ObjectInputValidation
java.lang.AutoCloseable
java.lang.Comparable
java.lang.Iterable
java.lang.Readable
java.lang.Runnable
java.lang.Thread.UncaughtExceptionHandler
java.lang.instrument.ClassFileTransformer
java.lang.management.PlatformManagedObject
java.lang.reflect.GenericArrayType
java.lang.reflect.InvocationHandler
java.net.ContentHandlerFactory
java.net.CookiePolicy
java.net.DatagramSocketImplFactory
java.net.FileNameMap
java.net.ProtocolFamily
java.net.SocketImplFactory
java.net.URLStreamHandlerFactory
java.nio.file.DirectoryStream.Filter
java.nio.file.PathMatcher
java.nio.file.WatchEvent.Modifier
java.nio.file.attribute.AttributeView
java.nio.file.attribute.FileAttributeView
java.nio.file.attribute.FileStoreAttributeView
java.rmi.activation.ActivationInstantiator
java.rmi.activation.Activator
java.rmi.server.RMIClientSocketFactory
java.rmi.server.RMIFailureHandler
java.rmi.server.RMIServerSocketFactory
java.rmi.server.Unreferenced
java.security.DomainCombiner
java.security.Guard
java.security.KeyStore.LoadStoreParameter
java.security.PrivilegedAction
java.security.PrivilegedExceptionAction
java.security.cert.CertPathParameters
java.security.cert.CertPathValidatorResult
java.security.cert.CertStoreParameters
java.security.interfaces.DSAKey
java.security.interfaces.ECKey
java.security.interfaces.RSAKey
java.security.spec.ECField
java.sql.DriverAction
java.time.chrono.Era
java.time.temporal.TemporalAdjuster
java.time.temporal.TemporalQuery
java.util.Formattable
java.util.Observer
java.util.concurrent.Callable
java.util.concurrent.Executor
java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory
java.util.concurrent.RejectedExecutionHandler
java.util.concurrent.ThreadFactory
java.util.function.BiConsumer
java.util.function.BiFunction
java.util.function.BiPredicate
java.util.function.BinaryOperator
java.util.function.BooleanSupplier
java.util.function.Consumer
java.util.function.DoubleBinaryOperator
java.util.function.DoubleConsumer
java.util.function.DoubleFunction
java.util.function.DoublePredicate
java.util.function.DoubleSupplier
java.util.function.DoubleToIntFunction
java.util.function.DoubleToLongFunction
java.util.function.DoubleUnaryOperator
java.util.function.Function
java.util.function.IntBinaryOperator
java.util.function.IntConsumer
java.util.function.IntFunction
java.util.function.IntPredicate
java.util.function.IntSupplier
java.util.function.IntToDoubleFunction
java.util.function.IntToLongFunction
java.util.function.IntUnaryOperator
java.util.function.LongBinaryOperator
java.util.function.LongConsumer
java.util.function.LongFunction
java.util.function.LongPredicate
java.util.function.LongSupplier
java.util.function.LongToDoubleFunction
java.util.function.LongToIntFunction
java.util.function.LongUnaryOperator
java.util.function.ObjDoubleConsumer
java.util.function.ObjIntConsumer
java.util.function.ObjLongConsumer
java.util.function.Predicate
java.util.function.Supplier
java.util.function.ToDoubleBiFunction
java.util.function.ToDoubleFunction
java.util.function.ToIntBiFunction
java.util.function.ToIntFunction
java.util.function.ToLongBiFunction
java.util.function.ToLongFunction
java.util.function.UnaryOperator
java.util.logging.Filter
java.util.prefs.PreferenceChangeListener
java.util.spi.ResourceBundleControlProvider
javax.accessibility.Accessible
javax.activation.CommandObject
javax.activation.DataContentHandlerFactory
javax.imageio.IIOParamController
javax.imageio.event.IIOReadWarningListener
javax.imageio.event.IIOWriteWarningListener
javax.imageio.metadata.IIOMetadataController
javax.imageio.spi.ServiceRegistry.Filter
javax.management.DescriptorRead
javax.management.NotificationFilter
javax.management.NotificationListener
javax.management.openmbean.CompositeDataView
javax.management.remote.JMXAddressable
javax.management.remote.JMXAuthenticator
javax.management.remote.JMXConnectorProvider
javax.management.remote.JMXConnectorServerProvider
javax.naming.NameParser
javax.naming.Referenceable
javax.naming.event.NamingListener
javax.naming.ldap.HasControls
javax.naming.spi.InitialContextFactory
javax.naming.spi.InitialContextFactoryBuilder
javax.naming.spi.ObjectFactory
javax.naming.spi.ObjectFactoryBuilder
javax.naming.spi.StateFactory
javax.net.ssl.HandshakeCompletedListener
javax.net.ssl.HostnameVerifier
javax.print.FlavorException
javax.print.event.PrintJobAttributeListener
javax.print.event.PrintServiceAttributeListener
javax.security.auth.callback.CallbackHandler
javax.sound.midi.ControllerEventListener
javax.sound.midi.MetaEventListener
javax.sound.sampled.LineListener
javax.sql.RowSetReader
javax.sql.RowSetWriter
javax.swing.JComboBox.KeySelectionManager
javax.swing.ListCellRenderer
javax.swing.Painter
javax.swing.UIDefaults.ActiveValue
javax.swing.UIDefaults.LazyValue
javax.swing.event.CaretListener
javax.swing.event.ChangeListener
javax.swing.event.HyperlinkListener
javax.swing.event.ListSelectionListener
javax.swing.event.RowSorterListener
javax.swing.event.TableModelListener
javax.swing.event.TreeSelectionListener
javax.swing.event.UndoableEditListener
javax.swing.table.TableCellRenderer
javax.swing.text.Highlighter.HighlightPainter
javax.swing.text.Position
javax.swing.text.TabExpander
javax.swing.text.ViewFactory
javax.swing.tree.RowMapper
javax.swing.tree.TreeCellRenderer
javax.tools.DiagnosticListener
javax.tools.OptionChecker
javax.xml.bind.ValidationEventHandler
javax.xml.crypto.KeySelectorResult
javax.xml.crypto.NodeSetData
javax.xml.crypto.URIDereferencer
javax.xml.crypto.XMLStructure
javax.xml.stream.EventFilter
javax.xml.stream.StreamFilter
javax.xml.stream.XMLReporter
javax.xml.stream.XMLResolver
javax.xml.stream.util.XMLEventConsumer
javax.xml.transform.URIResolver
javax.xml.ws.AsyncHandler
javax.xml.ws.Provider
javax.xml.ws.handler.HandlerResolver
javax.xml.xpath.XPathFunction
javax.xml.xpath.XPathFunctionResolver
javax.xml.xpath.XPathVariableResolver
org.omg.CORBA.DomainManagerOperations
org.omg.CORBA.portable.InvokeHandler
org.omg.CORBA.portable.ValueBase
org.omg.CORBA.portable.ValueFactory
org.omg.IOP.CodecFactoryOperations
org.omg.PortableInterceptor.PolicyFactoryOperations
org.omg.PortableServer.AdapterActivatorOperations
org.w3c.dom.DOMErrorHandler
org.w3c.dom.UserDataHandler
org.w3c.dom.events.DocumentEvent
org.w3c.dom.events.EventListener
org.w3c.dom.ls.LSResourceResolver
org.w3c.dom.views.AbstractView
org.w3c.dom.views.DocumentView
org.xml.sax.EntityResolver
Dampproof answered 23/3, 2017 at 10:46 Comment(0)
U
6

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
Unholy answered 22/3, 2017 at 3:57 Comment(6)
Which one of the two ideas? Eclipse I guess?Peluso
If you were Jon Skeet you would have coded that search utility yourself (the more complicated one of course).Peluso
Alas. No. Nobody will ever get there.Peluso
yes... that's probably a problem of the stackoverflow system... he will probably still make 200 points per day even if he is not alive anymore... and any challenger will make 200 points at most... so... will probably take some decades until his answers aren't that valid anymore (even get downvoted then?), but those of the challengers still need to be up to date. But who knows. Maybe the community is then contributing to his answers due to his efforts for when he was alive and they will build statues and such things ;-)Boat
@Boat It isn't that simple. When you look at this quarter for example, you will find that Jon is "only" on position 14 in that ranking. And the top person was collecting 10K more than Jon. And that top Gordon is just 400K behind Jon. So that makes 40 quarters until he is where Jon is today. Uups; wait a minute. And just for the record: I managed to get into the top 30 or so some other month; and be assured: it is a hell lot of work. But you are also correct: Jon is able to collect a lot of "interested" from his zillions of postings. And he has a cute avatar.Peluso
Thank you @shmosel. Seems to match well the list on the Use link posted previously.Jugate
P
5

Workaround: you might be able to use eclipse for example to gather such a list.

Simply jump into the source of that annotation and then search globally for its usage.

Alternatively you could use reflection and write code to scan all classes in some JAR to check each class if it is using that annotation. That would require some effort, but I don't see any major obstacles getting there; it's just about sitting down and doing it.

But of course, the real answer might be: this is probably a xy problem; and we should rather focus on the "why" you think you need to know about this.

Peluso answered 22/3, 2017 at 3:47 Comment(8)
Reflection would be much more useful for another purpose: it's (probably) the only way to find valid functional interfaces not explicitly marked with the annotation. Of course that would require more complicated analysis of class hierarchy, default methods, Object methods, etc.Unholy
Agreed. Also regarding the complexity of such an "extended" search routine.Peluso
…or you go to the official API documentation, browse to the FunctionalInterface and click on the USE link at the top. It’s really easy…Dampproof
That is super kewl. I actually went to the javadoc ... but on my mobile phone; I didn't see that link. So I assumed that annotations (in contrast to classes/interfaces) do not have that "used here" information! Kewl. You should make that an answer!Peluso
@Dampproof - YES! Thank you. Just what I was looking for :-) If you add your solution as an "answer" instead of a comment I can make it the answer :-) (at least as far as I'm concerned it is). As to the why: learning functional interfaces and thought it would be useful to be able to examine them in use in the language. Thank you!!Jugate
@Elisabeth: keep in mind that the annotation is not required for a functional 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 few of them have the annotation. Some of them work smoothly despite not having the annotation, e.g. ActionListener, InvocationHandler, or ThreadFactory, others are unsuitable like Comparable, ProtocolFamily, FlavorException. See also this questionDampproof
@Holger: thank you for that clarification. I guessed as much when I didn't see ActionListener on the list. It would be great if they were all identified at some point. But this is a very helpful start - thank you so much.Jugate
I added a complete list. Of course, there might be more when adding commonly used 3rd party libraries…Dampproof
B
3

Here's a link to the Uses page of @FunctionalInterface: Uses of Class @FunctionalInterface

Below are all commonly used and semantically neutral ones, listed with their argument and return types.

Full list available here: List of Functional Interfaces (programming.guide)

RunnableBiConsumer<T, U>                    T, U →
BiFunction<T, U, R>                 T, U → R
BinaryOperator<T>                   T, T → T
BiPredicate<T, U>                   T, U → boolean
BooleanSupplier                          → boolean
Consumer<T>                            T →
DoubleBinaryOperator      double, double → double
DoubleConsumer                    double →
DoubleFunction<R>                 double → R
DoublePredicate                   double → boolean
DoubleSupplier                           → double
DoubleToIntFunction               double → int
DoubleToLongFunction              double → long
DoubleUnaryOperator               double → double
Function<T, R>                         T → R
IntBinaryOperator                    int → int
IntConsumer                          int →
IntFunction<R>                       int → R
IntPredicate                         int → boolean
IntSupplier                              → int
IntToDoubleFunction                  int → double
IntToLongFunction                    int → long
IntUnaryOperator                     int → int
LongBinaryOperator            long, long → long
LongConsumer                        long →
LongFunction<R>                     long → R
LongPredicate                       long → boolean
LongSupplier                             → long
LongToDoubleFunction                long → double
LongToIntFunction                   long → int
LongUnaryOperator                   long → long
ObjDoubleConsumer<T>           T, double →
ObjIntConsumer<T>                 T, int →
ObjLongConsumer<T>               T, long →
Predicate<T>                           T → boolean
Supplier<T>                              → T
ToDoubleBiFunction<T, U>            T, U → double
ToDoubleFunction<T>                    T → double
ToIntBiFunction<T, U>               T, U → int
ToIntFunction<T>                       T → int
ToLongBiFunction<T, U>              T, U → long
ToLongFunction<T>                      T → long
UnaryOperator<T>                       T → T
Brent answered 20/11, 2019 at 2:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.