Java lambda表达式,转换和比较器 [英] Java lambda expressions, casting, and Comparators

查看:1032
本文介绍了Java lambda表达式,转换和比较器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 Map 界面的Java源代码,并遇到了这段代码:

I was looking through Java source code for the Map interface and ran into this little snippet of code:

    /**
     * Returns a comparator that compares {@link Map.Entry} in natural order on value.
     *
     * <p>The returned comparator is serializable and throws {@link
     * NullPointerException} when comparing an entry with null values.
     *
     * @param <K> the type of the map keys
     * @param <V> the {@link Comparable} type of the map values
     * @return a comparator that compares {@link Map.Entry} in natural order on value.
     * @see Comparable
     * @since 1.8
     */
    public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
        return (Comparator<Map.Entry<K, V>> & Serializable)
            (c1, c2) -> c1.getValue().compareTo(c2.getValue());
    }

从方法声明中我得到这是一个返回比较器的通用方法一种类型,可以从传递给它的地图条目中推断出来,也可以在方法中明确提供。

From the method declaration I get that this is a generic method that returns a Comparator of a type that is either inferred from the map entries passed to it or explicitly provided in the method.

真正让我失望的是返回值。似乎lambda表达式

What's really throwing me off is the return value. It appears that the lambda expression

(c1, c2) -> c1.getValue().compareTo(c2.getValue());

显式地转换为比较器< Map.Entry< K,V> > 。这是对吗?

is explicitly cast to a Comparator<Map.Entry<K, V>>. Is this right?

我还注意到,明显的演员阵容包括&序列化。我以前从未在演员表中看到过与某个类相结合的接口,但在编译器中看起来如下有效:

I also noticed that the apparent cast includes & Serializable. I've never seen an interface combined with a class in a cast before, but it looks like the following valid in the compiler:

(( SubClass& AnInterface)anObject).interfaceMethod();

虽然以下内容不起作用:

Although the following doesn't work:

public class Foo {
    public static void main(String[] args) {
        Object o = new Foo() {
            public void bar() {
                System.out.println("nope");
            }
        };
        ((Foo & Bar) o).bar();
    }   
}

interface Bar {
    public void bar();
}

所以,有两个问题:


  1. 如何为演员表添加界面?这只是强制执行接口方法的返回类型吗?

  1. How does adding an interface to a cast supposed to work? Does this just enforce the return type of an interface's method?

可以将Lambda表达式转换为比较器?他们还可以演绎什么呢?或者lambda表达式本质上只是比较器?有人可以澄清所有这些吗?

Can you cast a Lambda expression to a Comparator? What else can they be cast as? Or is a lambda expression essentially just a Comparator? Can someone clarify all of this?


推荐答案


如何在演员表中添加界面?

How does adding an interface to a cast supposed to work?

这具有强制转换的语法,但它实际上是定义了您通过类​​型接口创建的lambda的类型。即你没有创建一个对象的实例,然后被转换为另一种类型。

This has the syntax of a cast, however it is actually defining the type of the lambda you are creating via type interface. I.e. you are not creating an instance of a object which is then being cast to another type.


这是否只强制执行接口的返回类型方法?

Does this just enforce the return type of an interface's method?

这实际上定义了lambda将在运行时构建的类型。有一个 LambdaMetaFactory ,它在运行时获取此类型并生成额外的代码,如果类型包括 Serializable

This actually defines the type the lambda will be built as at runtime. There is a LambdaMetaFactory which obtains this type at runtime and generates extra code if the type includes Serializable.


你能否将一个Lambda表达式转换为比较器?

Can you cast a Lambda expression to a Comparator?

您只能对对象已经存在的类型进行引用。在这种情况下,您定义要创建的lambda必须是 Comparator 。你可以使用任何只有一种抽象方法的类型。

You can only cast a reference to a type the object is already. In this case you are defining the lambda to be created must be Comparator. You can use any type which has exactly one abstract method.


或者lambda表达式本质上只是一个比较器?

Or is a lambda expression essentially just a Comparator?

可以在不同的上下文和不同的接口中使用相同的lambda代码(复制+粘贴)而无需更改。它不必是 Comparator ,您将在JDK中的许多其他示例中看到。

The same lambda code could be used (copy+pasted) in different contexts and different interfaces without change. It doesn't have to be a Comparator as you will see in many other examples in the JDK.

一个我觉得有趣的是上的 count 方法。

One I find interesting is the count method on a Stream.

这篇关于Java lambda表达式,转换和比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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