您是否有 Java 8 个功能接口的列表(不是 java.util.function 中列出的那些)?

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

我正在尝试查看是否有任何方法可以获取 Java 8 中所有功能接口的列表。我不是在谈论此页面上的列表:

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

相反,我说的是 Comparator、FileFilter 和 Runnable 等接口 - API 文档显示的接口功能如下:

@FunctionalInterface public 界面可运行

哪里有这些的完整列表?

谢谢!

解决方法:例如,您可以使用 eclipse 来收集这样的列表。

只需跳转到该注释的来源,然后在全球范围内搜索其用法。

或者,您可以使用反射并编写代码来扫描某些 JAR 中的所有 classes,以检查每个 class 是否正在使用该注释。这需要付出一些努力,但我没有看到任何重大障碍;这只是坐下来做。

当然,真正的答案可能是:这可能是一个xy问题;我们应该关注您认为需要了解的 "why"。

使用 Eclipse方法,这里是运行时库中标记为@FunctionalInterface的接口的实际列表,不包括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

当您浏览到顶部的 FunctionalInterface’s class documentation and click on the USE link 时,API 文档中提供了所有用 @FunctionalInterface 注释的接口的列表。

但必须强调的是,注解的存在是not mandatory to make an interface a functional interface。每个接口只有一个 abstract 方法与 java.lang.Objectpublic 方法不匹配,可以通过 lambda 表达式或方法引用来实现,尽管这并不一定意味着结果将满足额外的要求为特定 interface.

指定的合同

, 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 “

因此,虽然 @FunctionalInterface 记录了可用作 lambda 表达式或方法引用的目标类型的意图,但其他接口类型可能仍适用于相同的目的,但您必须自己调查合同判断使用是否合适

为了完整起见,这里列出了可以通过 lambda 表达式或方法引用实现的所有 JRE 接口,但并非所有接口在以这种方式实现时都是有用的或在语义上是正确的。此列表不包括扩展 API,如 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

这是 @FunctionalInterface 的使用页面的 link:Class 的使用 @FunctionalInterface

下面是所有常用的和语义中性的,列出了它们的参数和 return 类型。

此处提供完整列表: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