How to use builder in IntStream?

Here is an example to explain how to use builder() in IntStream.

Syntax:

Modifier and Type                Method
static IntStream.Builder         builder()
package com.nourit.java8.Streams.IntStream;

import java.util.stream.IntStream;
import java.util.stream.Stream;

public class IntStreamBuilder {

	public static void main(String[] args) {
		System.out.println("---------- builder Example 1 -----------------");
		IntStream intStream1 = IntStream.builder().add(10).add(11).build();
		// Returns a builder for an IntStream.
		intStream1.forEach(System.out::println);
		System.out.println("---------- builder Example 2 -----------------");
		// calls the sum method to find the sum of the stream
		int sum = IntStream.builder().add(10).add(11).build().sum();
		System.out.println("sum = " +sum);
		System.out.println("---------- builder Example 3 -----------------");
		// A sream object without collection intermediate step
        Stream.builder()
                .add("we ")
                .add("build ")
                .add("a setence.")
                .build().forEachOrdered(System.out::print);
        System.out.println();
		System.out.println("---------------------------------------");
	}
}

Output:

---------- builder Example 1 -----------------
10
11
---------- builder Example 2 -----------------
sum = 21
---------- builder Example 3 -----------------
we build a setence.
---------------------------------------

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*