广州北大青鸟计算机职业培训学校
互联网技术培训、软件技术培训、大数据培训、云计算培训、数据分析培训信息网
当前位置:网站首页 > 软件教程 > Java技术 > 正文

惠州什么是Java流映射并行reduce?_北大青鸟IT学校

作者:邓华发布时间:2021-05-26分类:Java技术浏览:935


导读:什么是Java流映射并行reduce?相信学过Java的同学都遇到过这个问题,那么答案是什么呢?接下来一起来看看惠州北大青鸟老师是怎么回答的。

什么是Java流映射并行reduce?相信学过Java的同学都遇到过这个问题,那么答案是什么呢?接下来一起来看看惠州北大青鸟老师是怎么回答的。

Java Streams API支持并行映射缩减操作。

当使用以下reduce方法时,每个线程使用累加器累加部分结果。最后,组合器用于组合来自所有线程的部分结果以获得结果。

<U> U reduce(U identity, BiFunction<U,? super  T,U> accumulator, BinaryOperator<U> combiner)

以下代码显示了如何顺序并行reduce操作工作。

import java.time.LocalDate;import java.time.Month;import java.util.Arrays;import java.util.List;public class Main {  public static void main(String[] args) {    double sum = Employee
       .persons()
       .stream()
       .reduce(
           0.0,
           (Double partialSum, Employee p) -> {              double accumulated = partialSum + p.getIncome();
             System.out.println(Thread.currentThread().getName()
                 + "  - Accumulator: partialSum  = " + partialSum
                 + ",  person = " + p + ", accumulated = " + accumulated);              return accumulated;
           },
           (a, b) -> {              double combined = a + b;
             System.out.println(Thread.currentThread().getName()
                 + "  - Combiner:  a  = " + a + ", b  = " + b
                 + ", combined  = " + combined);              return combined;
           });
   System.out.println("--------------------------------------");
   System.out.println(sum);

   sum = Employee
       .persons()
       .parallelStream()
       .reduce(
           0.0,
           (Double partialSum, Employee p) -> {              double accumulated = partialSum + p.getIncome();
             System.out.println(Thread.currentThread().getName()
                 + "  - Accumulator: partialSum  = " + partialSum
                 + ",  person = " + p + ", accumulated = " + accumulated);              return accumulated;
           },
           (a, b) -> {              double combined = a + b;
             System.out.println(Thread.currentThread().getName()
                 + "  - Combiner:  a  = " + a + ", b  = " + b
                 + ", combined  = " + combined);              return combined;
           });
   System.out.println(sum);
 }
}class Employee {  public static enum Gender {
   MALE, FEMALE
 }  private long id;  private String name;  private Gender gender;  private LocalDate dob;  private double income;  public Employee(long id, String name, Gender gender, LocalDate dob,      double income) {
   this.id = id;
   this.name = name;
   this.gender = gender;
   this.dob = dob;
   this.income = income;
 }  public double getIncome() {    return income;
 }  public static List<Employee> persons() {
   Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971,
       Month.JANUARY, 1), 2343.0);
   Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972,
       Month.JULY, 21), 7100.0);
   Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973,
       Month.MAY, 29), 5455.0);
   Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974,
       Month.OCTOBER, 16), 1800.0);
   Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975,
       Month.DECEMBER, 13), 1234.0);
   Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976,
       Month.JUNE, 9), 3211.0);

   List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6);    return persons;
 }
}

上面的代码生成以下结果。

Java流组合实例.png

想了解更多关于Java的资讯,可以来惠州北大青鸟新方舟校区了解一下。

标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校


Java技术排行
标签列表
网站分类
文章归档
最近发表