了解 Java 8 Lambda 表达式

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

Understanding Java 8 Lambda Expressions

问题

Sure, here are the translations of the provided content:

  1. @FunctionalInterface声明对于以下代码是否必要?还是说没有它可以执行相同的代码,并且它只是用于文档目的?从文章中无法确定是否必要。

    @FunctionalInterface
    private interface DTOSender {
    void send(String accountId, DTO dto);
    }

    void sendDTO(BisnessModel object, DTOSender dtoSender) {
    //用于发送的一些逻辑...
    ...
    dtoSender.send(id, dto);
    ...
    }

  2. 通常情况下,可以在Java 8中将一个函数作为另一个函数的参数传递吗?我的理解是只能传递数据类型作为函数的参数,所以我认为函数不是数据类型,可能不行。

  3. 要实现上述问题2,我需要做一些特殊的事情吗?还是我只需编写这两个方法的定义,然后将一个方法作为参数传递给另一个方法即可?

  4. 在Java 8中,可以将对象作为另一个函数的参数传递吗?要实现这一点,我需要做一些特殊的事情吗?还是我只需编写对象和方法的定义,然后将对象作为参数传递给方法即可?

英文:

I was reading this article on Java 8 and had the following questions/comments I would appreciate some feedback/response.

  1. Is the @FunctionalInterface declaration necessary for the following code? Or could this same code be executed without it and it is for documentation purposes? It is unclear from whether it is necessary from the article.

    @FunctionalInterface
    private interface DTOSender {
    void send(String accountId, DTO dto);
    }

    void sendDTO(BisnessModel object, DTOSender dtoSender) {
    //some logic for sending...
    ...
    dtoSender.send(id, dto);
    ...
    }

  2. In general, can a function be passed as an argument to another function in Java 8? My understanding is only data types can be passed as arguments to functions, so I suppose it is not possible as a function is not a data type.

  3. Do I need to do anything special to accomplish #2 above or can I just write my definitions of the 2 methods and just pass the one method as a parameter to the other method?

  4. Can objects be passed as arguments to another function in Java 8? Do I need to do anything special to accomplish this or can I just write my definitions of the object and method and just pass the object as a parameter to the method?

答案1

得分: 3

  1. @Functional Interface只是一个提示,以便您不会将更多的方法放入您的接口。

  2. 可以的。Stream上的许多方法都将函数作为参数:Stream.of(1, 2, 3).forEach(System.out::println)

  3. Lambda是一个函数实例Function<Integer, Integer> f = a -> a + 1。编辑:您可以通过方法引用按名称传递函数(参见第2点,println是一个常规方法)。

  4. 我没有完全理解这个问题。如果方法消耗任何非原始类型的参数,它会接受一个对象(除了基本类型以外的所有东西在Java中都是对象)。

英文:
  1. @Functional Interface is just a hint, so that you don't put more methods into your interface.

  2. It can. Many methods on Stream take functions as parameter: Stream.of(1, 2, 3).forEach(System.out::println).

  3. Lambda is a function instance: Function&lt;Integer, Integer&gt; f = a -&gt; a + 1. Edit: you can pass a function by name using method reference (see 2., println is a regular method).

  4. I don't fully get the question. If the method consumes any argument, that is not primitive, it takes an object (everything in java except for primitives is an object).

答案2

得分: 2

[@FunctionalInterface](https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html)注解并非强制要求,正如文档中所述。

为了将函数传递给您的方法必须有一个匹配的函数式接口

interface ListFilter<T> {
    boolean test(T item);
}

public static <T extends Comparable<T>> List<T> filter(List<T> list, ListFilter<T> filter) {
    List<T> filteredList = new ArrayList<>();
    for (T t : list) {
        if (filter.test(t)) {
            filteredList.add(t);
        }
    }
    return filteredList;
}

public static boolean isNumberGreaterThan2(Integer integer){
    return integer > 2;
}

public static void main(String[] args) {
    List<Integer> list = List.of(1, 2, 3, 4);
    filter(list, new ListFilter<Integer>() {
        @Override
        public boolean test(Integer item) {
            return item > 2;
        }
    });
    // 或者
    filter(list, item -> item > 2);
    // 或者
    filter(list, Main::isNumberGreaterThan2);
}
英文:

The annotation @FunctionalInterface is not mandadory as the doc states.

In order to pass a function to your method, there has to be a matching functional interface.

interface ListFilter&lt;T&gt; {
    boolean test(T item);
}

public static &lt;T extends Comparable&lt;T&gt;&gt; List&lt;T&gt; filter(List&lt;T&gt; list, ListFilter&lt;T&gt; filter) {
    List&lt;T&gt; filteredList = new ArrayList&lt;&gt;();
    for (T t : list) {
        if (filter.test(t)) {
            filteredList.add(t);
        }
    }
    return filteredList;
}

public static boolean isNumberGreaterThan2(Integer integer){
    return integer &gt; 2;
}

public static void main(String[] args) {
    List&lt;Integer&gt; list = List.of(1, 2, 3, 4);
    filter(list, new ListFilter&lt;Integer&gt;() {
        @Override
        public boolean test(Integer item) {
            return item &gt; 2;
        }
    });
    // or
    filter(list, item -&gt; item &gt; 2);
    // or
    filter(list, Main::isNumberGreaterThan2);
}

huangapple
  • 本文由 发表于 2020年4月5日 21:33:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61043423.html
匿名

发表评论

匿名网友

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

确定