本文介绍了您是否有Java 8 Functional接口列表(不是java.util.function中列出的接口)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

相反,我是在谈论诸如Comparator,FileFilter和Runnable之类的接口-API文档显示的接口具有如下功能:

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

@FunctionalInterface公共接口Runnable

@FunctionalInterfacepublic interface Runnable

在某处有这些的完整列表吗?

Is there a full list of these somewhere?

谢谢!

推荐答案

当浏览到,然后单击 USE 链接.

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.

但是必须强调的是,注释的存在不是强制使interface成为功能性接口所必需的.每个接口只有一个abstract方法与java.lang.Objectpublic方法不匹配,可以通过lambda表达式或方法引用来实现,尽管这不一定意味着结果将满足为特定interface.

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.

JRE中有满足技术约束的JRE中大约有200个接口,因此当您尝试使用该编译器时,编译器不会反对通过lambda表达式实现它们.其中只有少数具有注释.一些没有注释的人仍然可以正常工作,例如ActionListenerInvocationHandlerThreadFactory,但由于诸如ComparableProtocolFamilyFlavorException之类的附加约束,其他选项不适用.在为什么在JDK中所有不合格的接口上都未使用@FunctionalInterface?"

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?"

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

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.

这篇关于您是否有Java 8 Functional接口列表(不是java.util.function中列出的接口)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 22:04