在本页中,我们将提供Java8
流flatMapToInt
、flatMapToLong
和flatMapToDouble
示例。flatMapToInt
用于int
数据类型,flatMapToLong
用于long
数据类型,flatMapToDouble
用于double
数据类型。它们的行为与flatMap
相同,但用于基本数据类型。
它用于原始数据类型int
。它返回IntStream
。
java文档原文。
'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,它由将提供的映射函数应用于每个元素所产生的映射流的内容替换此流的每个元素的结果组成。”
FlatMapToIntDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
public class FlatMapToIntDemo {
public static void main(String[] args) {
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("Ram", l1);
int[] l2 = {
2,7,8};
IntDemoPerson p2 = new IntDemoPerson("Shyam", 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
它用于原始数据类型long
。它返回LongStream
。
java文档原文。
"Returns an LongStream 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."
“返回一个LongStream,该LongStream由将提供的映射函数应用于每个元素而生成的映射流的内容替换此流的每个元素的结果组成。”
FlatMapToLongDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
import java.util.stream.LongStream;
public class FlatMapToLongDemo {
public static void main(String[] args) {
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("Ram", l1);
long[] l2 = {
2l,7l,8l};
LongDemoPerson p2 = new LongDemoPerson("Shyam", 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
它用于原始数据类型double
。它返回DoubleStream
。
java文档原文。
"Returns an DoubleStream 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"
“返回一个DoubleStream,它由将提供的映射函数应用于每个元素所产生的映射流的内容替换此流的每个元素的结果组成”
FlatMapToDoubleDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
import java.util.stream.DoubleStream;
public class FlatMapToDoubleDemo {
public static void main(String[] args) {
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
【1】Java 8 Stream flatMapToInt, flatMapToLong and flatMapToDouble Example