Java 8 Stream flatMapToInt, flatMapToLong and flatMapToDouble Example

By Arvind Rai, June 04, 2016
On this page we will provide Java 8 Stream flatMapToInt, flatMapToLong and flatMapToDouble example. flatMapToInt is used for int data type, flatMapToLong for long data type and flatMapToDouble for double data type. They behave same as flatMap but for primitive data types.

flatMapToInt

It is used for primitive int data type. It returns IntStream.
According to 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."

Now find the example.
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;
	}
} 
Output
21
9 

flatMapToLong

It is used for primitive long data type. It returns LongStream.
According to java doc.

"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."

Now find the example.
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;
	}
} 
Output
21
2 

flatMapToDouble

It is used for primitive double data type. It returns DoubleStream.
According to java doc.

"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"

Now find the example.
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;
	}
} 
Output
3.9166666666666665
58.9 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us