简体   繁体   中英

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.

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. There's not much difference between the above and 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. Don't do it. Love the 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. The answer is not 0. The answer is ¯\ (ツ) /¯.

Similarly, if somehow a null got into your list of persons, then the age of that person is also unknown. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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