如何将Comparator<T>视为函数式接口?

huangapple go评论92阅读模式
英文:

How is Comparator<T> a Functional Interface?

问题

根据函数式接口的定义 - 函数式接口是指只包含一个抽象方法的接口。

但是 Comparator<T> 却有两个抽象方法:

int compare(T o1, T o2);

boolean equals(Object obj);

其他的方法是默认方法和静态方法。

JavaDocs 将其标记为函数式接口。这是怎么回事呢?

英文:

According to definition of functional interface - A functional interface is an interface that contains only one abstract method.

But Comparator&lt;T&gt; has two abstract methods:

int compare(T o1, T o2);

boolean equals(Object obj);

others are default and static.

JavaDocs mentions it as functional interface. How can it be?

答案1

得分: 8

你正在阅读错误的定义,或者至少是一个(过于)简化的定义。

FunctionalInterface 的正确定义是:

函数式接口是指仅有一个抽象方法(除了 Object 的方法)的接口,从而表示单一的函数契约。这个“单一”方法可以采用从超级接口继承的具有覆盖等效签名的多个抽象方法的形式;在这种情况下,继承的方法在逻辑上代表一个单一方法。

来源:Java 语言规范第 9.8 节

英文:

You're reading the wrong definition, or at least, an (over)simplified one.

The proper definition of a FunctionalInterface is:

> A functional interface is an interface that has just one abstract method (aside from the methods of Object), and thus represents a single function contract. This "single" method may take the form of multiple abstract methods with override-equivalent signatures inherited from superinterfaces; in this case, the inherited methods logically represent a single method.

SOURCE: Java Language Specification section 9.8

答案2

得分: 4

代码部分不要翻译,只返回翻译好的内容:

如果您查看 `Comparator<T>` 的源代码,如下所示:

@FunctionalInterface
public interface Comparator<T> {

  // 抽象方法
  int compare(T o1, T o2);

  // 抽象方法,重写了 `java.lang.Object` 的公共方法,因此不算在内
  boolean equals(Object obj);

}

`equals` 是一个抽象方法,它重写了 `java.lang.Object` 的一个公共方法,这不算作抽象方法。

因此实际上,Comparator 只有一个抽象方法,即 `int compare(T o1, T o2)`,它符合函数式接口的定义。
英文:

If you look at the source code of Comparator&lt;T&gt; , it is like below:

@FunctionalInterface
public interface Comparator&lt;T&gt; {

  // abstract method
  int compare(T o1, T o2);

  // abstract method, overriding public methods of `java.lang.Object`, so it does not count
  boolean equals(Object obj);

}

The equals is an abstract method overriding one of the public methods of java.lang.Object, this doesn’t count as an abstract method.

So In fact The Comparator only has one abstract method i.e int compare(T o1, T o2), and it meet the definition of functional interface.

答案3

得分: 3

equals 只在 Comparator 接口中显式地包含,以便他们可以添加一些附加的 JavaDocs,例如一些特定于比较器的要求:

> 只有在指定的对象也是比较器并且它施加与此比较器相同的排序时,此方法才能返回 true。

来源:Comparator JavaDocs

JLS 表示:

> 接口不从 Object 继承,而是隐式声明了许多与 Object 相同的方法(§9.2)。

因此,所有 Comparator 的作者所做的只是显式地声明了通常是隐式的内容。

如果 Object 的方法被视为定义函数式接口目的的抽象方法,那么没有函数式接口会只有一个抽象方法。因此,它们不被视为:

> 函数式接口是仅具有一个抽象方法的接口**(Object 的方法除外)**。

JLS,强调是我的。

英文:

equals is only explicitly included in the Comparator interface so that they can add some additional JavaDocs to it, e.g. some comparator-specific requirements:

> this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator

Source: Comparator JavaDocs

The JLS says:

> interfaces do not inherit from Object, but rather implicitly declare
> many of the same methods as Object (§9.2)

So all the Comparator authors have done is declare something explicitly that is normally implicit.

If the methods of Object counted as abstract methods for the purposes of defining a functional interface then no functional interface would have just a single abstract method. As such, they are not considered:

> A functional interface is an interface that has just one abstract
> method (aside from the methods of Object)

JLS, emphasis mine

huangapple
  • 本文由 发表于 2020年8月26日 20:43:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63597966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定