package org.lxp.java8;

import java.util.Comparator;
import java.util.List;
import java.util.Stack;
import java.util.function.Function;

import org.lxp.vo.Student;

public class StudyFunction {
    public static int getTotalAge(List<Student> students) {
        return totalAgeFunction.apply(students);
    }

    private static Function<List<Student>, Integer> totalAgeFunction = students -> {
        Stack<Integer> stack = new Stack<>();
        stack.push(0);
        students.forEach(student -> stack.push(stack.pop() + student.getAge()));
        return stack.pop();
    };

    public static int getTotalAgeViaReduce1(List<Student> students) {
        return students.stream().map(Student::getAge).reduce(0, (a, b) -> a + b);
    }

    public static int getTotalAgeViaReduce2(List<Student> students) {
        return students.stream().map(Student::getAge).reduce(Integer::sum).orElse(1000);
    }

    public static int getTotalAgeViaReduce3(List<Student> students) {
        return students.stream().mapToInt(Student::getAge).sum();
    }

    public static Student getYoungestStudent(List<Student> students) {
        return students.stream().reduce((a, b) -> a.getAge() < b.getAge() ? a : b).orElse(null);
    }

    public static Student getOldestStudent(List<Student> students) {
        return students.stream().max(Comparator.comparing(Student::getAge)).orElse(null);
    }

    public static double getAverageAge(List<Student> students) {
        return students.stream().mapToInt(Student::getAge).average().orElse(0);
    }
}

@@ -187,16 +187,98 @@
 ction %7B%0A
+    /**%0A     * %E5%88%A9%E7%94%A8function%E6%B1%82%E5%92%8C%0A     * %0A     * @param students%0A     * @return%0A     */%0A
     publ
@@ -321,32 +321,32 @@
 nt%3E students) %7B%0A
-
         return t
@@ -656,16 +656,102 @@
     %7D;%0A%0A
+    /**%0A     * %E5%88%A9%E7%94%A8reduce%E6%B1%82%E5%92%8C%E6%96%B9%E6%B3%95%E4%B8%80 %E6%9C%80%E5%B7%AE%0A     * %0A     * @param students%0A     * @return%0A     */%0A
     publ
@@ -893,32 +893,117 @@
  a + b);%0A    %7D%0A%0A
+    /**%0A     * %E5%88%A9%E7%94%A8reduce%E6%B1%82%E5%92%8C%E6%96%B9%E6%B3%95%E4%BA%8C %E4%B8%AD%0A     * %0A     * @param students%0A     * @return%0A     */%0A
     public stati
@@ -1144,32 +1144,118 @@
 e(1000);%0A    %7D%0A%0A
+    /**%0A     * %E5%88%A9%E7%94%A8reduce%E6%B1%82%E5%92%8C%E6%96%B9%E6%B3%95%E4%B8%89 %E6%9C%80%E5%A5%BD%0A     * %0A     * @param students%0A     * @return%0A     */%0A
     public stati

add description