简体   繁体   English

如何将 function 作为方法参数传递?

[英]How to pass function as method parameter?

I have 2 methods for getting average age of woman, and second for getting average age of man.我有两种获取女性平均年龄的方法,第二种获取男性平均年龄的方法。 I need to do one method to agregate this 2 methods.我需要做一种方法来聚合这两种方法。

This method as first parameter should have list of person, and as a second parameter function what will decide if we are looking for man or woman average age.这个方法作为第一个参数应该有人员列表,作为第二个参数 function 什么将决定我们是在寻找男人还是女人的平均年龄。

This is my methods:这是我的方法:

public double getMenAverageAge(List<Person> personList){
    return Optional.ofNullable(personList)
            .orElseGet(ArrayList::new)
            .stream()
            .filter(Objects::nonNull)
            .filter(p -> !Boolean.parseBoolean(String.valueOf(p.getFirstName().endsWith("a"))))
            .mapToDouble(Person::getAge)
            .average()
            .orElse(0);
}

public double getAverageWomenAge(List<Person> personList){
    return Optional.ofNullable(personList)
            .orElseGet(ArrayList::new)
            .stream()
            .filter(Objects::nonNull)
            .filter(p-> Boolean.parseBoolean(String.valueOf(p.getFirstName().endsWith("a"))))
            .mapToDouble(Person::getAge)
            .average()
            .orElse(0);
}

Any ideas?有任何想法吗?

Predicate<Person> p = p-> Boolean.parseBoolean(String.valueOf(p.getFirstName().endsWith("a")));

Predicate<Person> is just a type. Predicate<Person>只是一种类型。 There's not much difference between the above and String x = "hello";上面和String x = "hello";没有太大区别. . You can make a method that accepts that type as param:您可以制作一个接受该类型作为参数的方法:

public double getAverageAge(List<Person> list, Predicate<Person> filter) {
  return list
            .stream()
            .filter(filter) 
            .mapToDouble(Person::getAge)
            .average()
            .orElse(0);

NB: You have null tourette, it's a formula for writing lots of code that is impossible to test.注意:您有 null 图雷特,它是编写大量无法测试的代码的公式。 Don't do it.不要这样做。 Love the NPE.喜欢 NPE。 The correct response to someone invoking this method passing in null for a list is to crash with an NPE: null is best treated as 'unset' / 'unknown', and it is not possible to get the average age of an unknown list.对有人调用此方法传递null的列表的正确响应是与 NPE崩溃:null 最好被视为“未设置”/“未知”,并且不可能获得未知列表的平均年龄。 The answer is not 0. The answer is ¯\ (ツ) /¯.答案不是0。答案是¯\ (ツ) /¯。

Similarly, if somehow a null got into your list of persons, then the age of that person is also unknown.同样,如果null以某种方式进入了您的人员列表,那么该人的年龄也是未知的。 It is not possible to determine the average age of a batch of persons when one of the persons in there is a complete unknown, again, just NPEing is in fact the right response.当其中一个人完全未知时,无法确定一批人的平均年龄,同样,NPEing 实际上是正确的反应。

The best way to deal with null in java is to use the tools to your advantage: If the NullPointerException is in fact the best thing that could happen, you're doing it right.处理 java 中的 null 的最佳方法是使用对您有利的工具:如果 NullPointerException 实际上是可能发生的最好的事情,那么您做对了。

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

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