简体   繁体   English

Lambda表达式在一个接口上工作吗?

[英]does Lambda expression work on one interface?

Java 7 Java 7

List<Person> personList = Person.createShortList();

// Sort with Inner Class
Collections.sort(personList, new Comparator<Person>() {
    public int compare(Person p1, Person p2) {
        return p1.getSurName().compareTo(p2.getSurName());
    }
});

Java 8 Java 8

Collections.sort(personList, (Person p1, Person p2) -> 
                 p1.getSurName().compareTo(p2.getSurName()));

for (Person p : personList) {
    p.printName();
}

If the interface Comparator has 2 methods and not just one compare is it possible to use Lambda ? 如果接口Comparator有2种方法,而不仅仅是一种compare ,则可以使用Lambda吗?

for example 例如

public interface Comparator<T> {
    int compare(T o1, T o2);
    int compareTest(T o1, T o2);
}

Functional interfaces need exactly one abstract method which is not the case for your interface. 功能接口仅需要一种抽象方法,而接口则不是这种方法。 However you could provide a default implementation for one of the methods. 但是,您可以为其中一种方法提供默认实现。 In that case it would work: 在这种情况下,它将起作用:

@FunctionalInterface
public interface Comparator<T> {

    int compare(T o1, T o2);

    default int reverseCompare(T o1, T o2) {
        return compare(o2, o1);
    }
}

Lambdas can be used where implementations of functional interfaces are expected. 可以在需要实现功能接口的地方使用Lambda。 The definition of a functional interface is in JLS§9.8 : 功能接口的定义在JLS§9.8中

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. 功能接口是仅具有一个抽象方法(除Object的方法之外)的接口,因此表示单个功能协定。 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. 在这种情况下,继承的方法在逻辑上表示单个方法。

So in your example, no, if there were two abstract methods on Comparator ( compare and compareTest ), you couldn't implement it using a lambda. 因此,在你的榜样,没有,如果有两个抽象方法ComparatorcomparecompareTest ),你不能使用Lambda实现它。

For example, this works: 例如,这有效:

@FunctionalInterface
interface Foo {
    void method1(int x);
}

public class Example {
    public static final void main(String[] args) {
        new Example().testFoo(x -> {
            System.out.println(x);
        });
    }

    private void testFoo(Foo f) {
        for (int x = 0; x < 5; ++x) {
            f.method1(x);
        }
    }
}

...but if we add to Foo : ...但是如果我们添加到Foo

// WON'T COMPILE
@FunctionalInterface
interface Foo {
    void method1(int x);
    void method2(int x);
}

...it won't compile anymore because it's no longer a functional interface. ...它将不再编译,因为它不再是功能接口。 (If we removed the annotation, the interface would compile [of course], but our use of it via a lambda wouldn't.) (如果删除了注释,则该接口将[当然]进行编译,但通过lambda对其进行使用则不会。)

Note that we could add a default method to Foo : 请注意,我们可以Foo添加默认方法:

@FunctionalInterface
interface Foo {
    void method1(int x);
    default void method2(int x) {
        // ...
    }
}

That's okay because there's only one abstract method on the interface. 可以,因为接口上只有一个抽象方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM