Java8中功能接口的用途 [英] Purpose of Functional Interfaces in Java8

查看:116
本文介绍了Java8中功能接口的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Java8 内置功能接口,我遇到过很多问题,包括这个这个这个。但所有人都在问为什么只有一种方法?或如果我使用我的功能界面执行X,为什么会出现编译错误等等。我的问题是:这些新功能接口的存在目的是什么,当我可以在我自己的界面中使用lambdas时

I've come across many questions in regards of Java8 in-built Functional Interfaces, including this, this, this and this. But all ask about "why only one method?" or "why do I get a compilation error if I do X with my functional interface" and alike. My question is: what is the existential purpose of these new Functional Interfaces, when I can use lambdas anyway in my own interfaces?

考虑一下以下来自 oracle文档的示例代码:

Consider the following example code from oracle documentation:

    // Approach 6: print using a predicate
     public static void printPersonsWithPredicate(List<Person> roster, 
                                                  Predicate<Person> tester) {
            for (Person p : roster) {
                if (tester.test(p)) {
                   System.out.println(p);
                }
            }
        }

好的,很棒,但是这个可以使用上面的示例实现(具有单个方法的接口并不新鲜):

OK, great, but this is achievable with their own example just above (an interface with a single method is nothing new):

      // Approach 5: 
        public static void printPersons(<Person> roster, 
                                        CheckPerson tester) {
            for (Person p : roster) {
                if (tester.test(p)) {
                   System.out.println(p);
                }
            }
        }


  interface CheckPerson {
        boolean test(Person p);
    }

我可以将lambda传递给两个方法。

I can pass a lambda to both methods.

第一种方法为我节省了一个自定义界面。 这是吗?

1st approach saves me one custom interface. Is this it?

或者这些标准功能接口(消费者,供应商,谓词,功能)是否可以作为代码的模板组织,可读性,结构,[其他]?

Or are these standard functional interfaces (Consumer, Supplier, Predicate, Function) are meant to serve as a template for code organization, readability, structure, [other]?

推荐答案

显然,您可以跳过使用这些新界面并使用更好的名称滚动自己的界面。但是有一些注意事项:

Obviously you can skip using these new interfaces and roll your own with better names. There are some considerations though:


  1. 除非您的自定义界面扩展了其中一个,否则您将无法在其他JDK API中使用自定义界面-ins。

  2. 如果你总是自己动手,在某些时候你会遇到一个你不能想到好名字的情况。例如,我认为 CheckPerson 对于它的目的来说并不是一个好名字,虽然这是主观的。

  1. You will not be able to use custom interface in some other JDK API unless your custom interface extends one of built-ins.
  2. If you always roll with your own, at some point you will come across a case where you can't think of a good name. For example, I'd argue that CheckPerson isn't really a good name for its purpose, although that's subjective.

大多数内置接口也定义了一些其他API。例如,谓词定义或(谓词)和(谓词) negate()

Most builtin interfaces also define some other API. For example, Predicate defines or(Predicate), and(Predicate) and negate().

功能定义 andThen(Function) compose(Function)等。

这并不是特别令人兴奋,直到它是:在函数上使用除抽象之外的方法允许更容易的组合,策略选择等等,(使用这篇文章):

It's not particularly exciting, until it is: using methods other than abstract ones on functions allows for easier composition, strategy selections and many more, such as (using style suggested in this article):

之前:

class PersonPredicate {
  public Predicate<Person> isAdultMale() {
    return p -> 
            p.getAge() > ADULT
            && p.getSex() == SexEnum.MALE;
  }
}

可能只是变成了这个,它更可重复使用结束:

Might just become this, which is more reusable in the end:

class PersonPredicate {
  public Predicate<Person> isAdultMale() {
    return isAdult().and(isMale());
  }

  publci Predicate<Person> isAdultFemale() {
    return isAdult().and(isFemale());
  }

  public Predicate<Person> isAdult() {
    return p -> p.getAge() > ADULT;
  }

  public Predicate<Person> isMale() {
    return isSex(SexEnum.MALE);
  }
  public Predicate<Person> isFemale() {
    return isSex(SexEnum.FEMALE);
  }
  public Predicate<Person> isSex(SexEnum sex) {
    return p -> p.getSex() == sex;
  }
}

这篇关于Java8中功能接口的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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