Stream是Java 8的新特性,基于lambda表达式,是对集合对象功能的增强,它专注于对集合对象进行各种高效、方便聚合操作或者大批量的数据操作,提高了编程效率和代码可读性。Collectors通常在Stream处理后,返回转换成集合类时使用,本文主要介绍Java Stream中Collectors.summarizingDouble/Long/Int()、Collectors.averagingDouble/Long/Int()和Collectors.summingDouble/Long/Int()的使用,,以及相关的示例代码。

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) {

    List<Person> pList = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55));
    List<Person> 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) {

    List<Person> pList = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55));
    List<Person> 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) {

    List<Person> pList = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55));
    List<Person> 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
  }
}

推荐文档

相关文档

大家感兴趣的内容

随机列表