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!
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.
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
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.
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
来源:https://stackoverflow.com/questions/42942351/do-you-have-a-list-of-java-8-functional-interfaces-not-the-ones-listed-in-java