为什么可以将lambda表达式用作比较器? [英] Why can a lambda expression be used as a Comparator?

查看:222
本文介绍了为什么可以将lambda表达式用作比较器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OCP学习指南一书中有一个有关Comparator的示例,该示例可以通过两种方式初始化.第一种是通过这样的匿名类:

In the book OCP Study Guide there is this example about a Comparator that can be initialized in two ways. The first is via an anonymous class like this:

Comparator<Duck> byWeight = new Comparator<Duck>(){
    public int compare(Duck d1, Duck d2){
        return d1.getWeight() - d2.getWeight();
    }
};

我可以理解.根据这本书,可以将其替换为以下lambda表达式:

This I can understand. According to the book this can be replaced with a lambda expression like this:

Comparator<Duck> byWeight = (d1,d2) -> d1.getWeight() - d2.getWeight();

现在我不明白. lambda表达式不会返回Comparator对象,因为Comparator是一个接口,所以我现在想到了它.

Now this I don't understand. The lambda expression does not return a Comparator object, which it couldn't now that I think of it since Comparator is an interface.

第一个示例中的new运算符是否引用正在创建的称为Comparator的匿名类,因为该匿名类实现了Comparator接口?

So does the new operator in the first example refer to the anonymous class that is being made which is called Comparator because that anonymous class implements the Comparator interface?

那么,示例2中到底发生了什么?是否通过lambda表达式以某种方式创建了对象?在此示例中,您将byWeight用作参考变量吗?

What exactly is happening in example 2 then? Is an object created somehow out of the lambda expression? In this example you use byWeight as a reference variable right?

我真的不明白,有人可以解释吗?谢谢.

I really don't understand this, could anyone please explain? Thank you.

推荐答案

如果您阅读了

If you read the documentation of the Comparator interface, you can read:

功能界面: 这是一个功能接口,因此可以用作lambda表达式或方法引用的分配目标.

Functional Interface: This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

Comparator<T>接口因此是现在,如果我们看一下 我们看到:

Now if we look at the documentation of @FunctionalInterface we see:

一种信息丰富的注释类型,用于指示接口类型 声明旨在成为功能接口,具体由 Java语言规范.从概念上讲,功能界面具有 完全是一种抽象方法.由于默认方法具有 实施,它们不是抽象的.如果接口声明一个 覆盖公共方法之一的抽象方法 java.lang.Object,这也不会计入接口的 抽象方法的数量,因为该接口的任何实现都会 可以从java.lang.Object或其他任何地方实现.

An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

因此,基本上,如果您有一个使用 one 抽象方法的接口,并且将该接口注释为@FunctionalInterface,则该接口是功能的目标:因为您或多或少构造了一个实现功能接口的匿名类,您指定的功能是唯一抽象方法的实现.

So basically if you have an interface with one abstract method, and you annotate the interface as a @FunctionalInterface, then that interface is a target for functions: in that you more or less construct an anonymous class that implements the functional interface and the function you specify is the implementation of the only abstract method.

换句话说,表达式:

Comparator<Duck> byWeight = <somelambda>

等同于 :

Comparator<Duck> byWeight = new Comparator<Duck>(){
    public int compare(Duck d1, Duck d2){
        return <somelambda>(d1,d2);
    }
}

这篇关于为什么可以将lambda表达式用作比较器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆