Collectors summarizingLong (ToLongFunction mapper)
Description
Collectors summarizingLong(ToLongFunction<? super T> mapper)
returns a Collector which applies an long-producing mapping function, and returns summary statistics for the resulting values.
Syntax
summarizingLong
has the following syntax.
public static <T> Collector<T,?,LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)
Example
The following example shows how to use summarizingLong
.
import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.stream.Collectors;
/* w ww. jav a 2 s .c o m*/
public class Main {
public static void main(String[] args) {
LongSummaryStatistics incomeStats = Employee.persons()
.stream()
.collect(Collectors.summarizingLong(Employee::getIncome));
System.out.println(incomeStats);
}
}
class Employee {
public static enum Gender {
MALE, FEMALE
}
private long id;
private String name;
private Gender gender;
private LocalDate dob;
private long income;
public Employee(long id, String name, Gender gender, LocalDate dob,
long income) {
this.id = id;
this.name = name;
this.gender = gender;
this.dob = dob;
this.income = income;
}
public long getIncome() {
return income;
}
public static List<Employee> persons() {
Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971,
Month.JANUARY, 1), 2343);
Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972,
Month.JULY, 21), 7100);
Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973,
Month.MAY, 29), 5455);
Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974,
Month.OCTOBER, 16), 1800);
Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975,
Month.DECEMBER, 13), 1234);
Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976,
Month.JUNE, 9), 3211);
List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6);
return persons;
}
}
The code above generates the following result.