1、Collectors.summarizingDouble/Long/Int()的使用
SummarizingDouble()
、SummarizingLong()
和SummarizingInt()
为stream中的元素生成了统计信息,返回的结果是一个统计类,例如,
import java.util.*;import java.util.stream.Collectors;import java.util.stream.Stream;public class Main { public static class Person { int age; Person (int age) { this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}public static void main(String[] args) { ListpList = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55)); List result = pList.stream() .map(o -> { o.setAge(18); return o; }).collect(Collectors.toList()); IntSummaryStatistics intResult = pList.stream() .collect(Collectors.summarizingInt(Person::getAge)); System.out.println("stream() result :" + intResult); LongSummaryStatistics longResult = pList.stream() .collect(Collectors.summarizingLong(Person::getAge)); System.out.println("stream() result :" + longResult); DoubleSummaryStatistics doubleResult = pList.stream() .collect(Collectors.summarizingDouble(Person::getAge)); System.out.println("stream() result :" + doubleResult); System.exit(0); //success }}
2、Collectors.averagingDouble/Long/Int()的使用
averagingDouble()
、averagingLong()
和averagingInt()
对stream中的元素计算平均,例如,
import java.util.*;import java.util.stream.Collectors;import java.util.stream.Stream;public class Main { public static class Person { int age; Person (int age) { this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}public static void main(String[] args) { ListpList = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55)); List result = pList.stream() .map(o -> { o.setAge(18); return o; }).collect(Collectors.toList()); Double intResult = pList.stream() .collect(Collectors.averagingInt(Person::getAge)); System.out.println("stream() result :" + intResult); Double longResult = pList.stream() .collect(Collectors.averagingLong(Person::getAge)); System.out.println("stream() result :" + longResult); Double doubleResult = pList.stream() .collect(Collectors.averagingDouble(Person::getAge)); System.out.println("stream() result :" + doubleResult); System.exit(0); //success }}
3、Collectors.summingDouble/Long/Int()的使用
summingDouble()
、summingLong()
和summingInt()
对stream中的元素做sum计算,如下,
import java.util.*;import java.util.stream.Collectors;import java.util.stream.Stream;public class Main { public static class Person { int age; Person (int age) { this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}public static void main(String[] args) { ListpList = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55)); List result = pList.stream() .map(o -> { o.setAge(18); return o; }).collect(Collectors.toList()); Integer intResult = pList.stream() .collect(Collectors.summingInt(Person::getAge)); System.out.println("stream() result :" + intResult); Long longResult = pList.stream() .collect(Collectors.summingLong(Person::getAge)); System.out.println("stream() result :" + longResult); Double doubleResult = pList.stream() .collect(Collectors.summingDouble(Person::getAge)); System.out.println("stream() result :" + doubleResult); System.exit(0); //success }}