1. 简介

在本文中,我们介绍Java 8 Stream flatMapToIntflatMapToLongflatMapToDouble方法。

  • flatMapToInt用于int数据类型
  • flatMapToLong用于长数据类型
  • flatMapToDouble用于双精度数据类型

它们的行为与flatMap方法相同,但适用于原始数据类型。关于flatMap方法的介绍可以参考这里

2. flatMapToInt

用于原始int数据类型。返回IntStream

根据java doc的描述:

Returns an IntStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.

返回一个IntStream,该结果包含将流中的每个元素替换为通过将提供的映射函数应用于每个元素而生成的映射流的内容而得到的结果。

我们来个简单的例子:

@Test
public void test_FlatMapToInt(){
  int[][] data = {{1,2},{3,4},{5,6}};

  IntStream is1 = Arrays.stream(data).flatMapToInt(row -> Arrays.stream(row));
  System.out.println(is1.sum());

  int[] l1 = {4,8,9};
  IntDemoPerson p1 = new IntDemoPerson("li", l1);
  int[] l2 = {2,7,8};
  IntDemoPerson p2 = new IntDemoPerson("sun", l2);
  List<IntDemoPerson> list = Arrays.asList(p1,p2);

  IntStream is2 = list.stream().flatMapToInt(row -> Arrays.stream(row.getLuckyNum()));
  System.out.println(is2.max().getAsInt());
}

class IntDemoPerson {
  private String name;
  private int[] luckyNum;
  public IntDemoPerson(String name, int[] luckyNum){
    this.name = name;
    this.luckyNum = luckyNum;
  }
  public String getName() {
    return name;
  }
  public int[] getLuckyNum() {
    return luckyNum;
  }
}

输出如下:

21
9

2. flatMapToLong

flatMapToInt方法类似,只不过用于基本类型long, 并返回LongStream

我们来看一个简单的例子:

@Test
public void test_FlatMapToLong(){
  long[][] data = {{1L,2L},{3L,4L},{5L,6L}};
  LongStream ls1 = Arrays.stream(data).flatMapToLong(row -> Arrays.stream(row));
  System.out.println(ls1.sum());

  long[] l1 = {4l,8l,9l};
  LongDemoPerson p1 = new LongDemoPerson("li", l1);
  long[] l2 = {2l,7l,8l};
  LongDemoPerson p2 = new LongDemoPerson("sun", l2);
  List<LongDemoPerson> list = Arrays.asList(p1,p2);

  LongStream ls2 = list.stream().flatMapToLong(row -> Arrays.stream(row.getLuckyNum()));
  System.out.println(ls2.min().getAsLong());
}

class LongDemoPerson {
  private String name;
  private long[] luckyNum;
  public LongDemoPerson(String name, long[] luckyNum){
    this.name = name;
    this.luckyNum = luckyNum;
  }
  public String getName() {
    return name;
  }
  public long[] getLuckyNum() {
    return luckyNum;
  }
}

输出如下:

21
2 

3. flatMapToDouble

flatMapToDouble方法类似,只不过用于基本类型double, 并返回DoubleStream

我们还是来上面例子:

@Test
public void  test_flatMapToDouble(){
  double[][] data = {{1.5,2.4},{3.2,4.4},{5.2,6.8}};
  DoubleStream ds1 = Arrays.stream(data).flatMapToDouble(row -> Arrays.stream(row));
  System.out.println(ds1.average().getAsDouble());

  double[] d1 = {60.5,58.9, 62.5};
  DoubleDemoPerson p1 = new DoubleDemoPerson("Ram", d1);
  double[] d2 = {70.5,65.3,67.4};
  DoubleDemoPerson p2 = new DoubleDemoPerson("Shyam", d2);
  List<DoubleDemoPerson> list = Arrays.asList(p1,p2);
  DoubleStream ds2 = list.stream().flatMapToDouble(row -> Arrays.stream(row.getWeightsInAYear()));
  System.out.println(ds2.min().getAsDouble());
}

class DoubleDemoPerson {
  private String name;
  private double[] weightsInAYear;
  public DoubleDemoPerson(String name, double[] weightsInAYear){
    this.name = name;
    this.weightsInAYear = weightsInAYear;
  }
  public String getName() {
    return name;
  }
  public double[] getWeightsInAYear() {
    return weightsInAYear;
  }
}

输出如下:

3.9166666666666665
58.9

4. 总结

在本文中,我们介绍Java 8 Stream flatMapToIntflatMapToLongflatMapToDouble方法。

它们的行为与flatMap方法相同,但适用于原始数据类型。

与往常一样,可以在GitHub上获得代码示例。