How to use asLongStream in IntStream?

Here is an example to explain how to use asLongStream in IntStream.

Syntax:

Modifier and Type         Method
LongStream                asLongStream()
package com.nourit.java8.Streams.IntStream;

import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class IntStreamAsLongStream {

	public static void main(String[] args) {
		System.out.println("---------- First Example -----------------");
		IntStream i = IntStream.of(0, 1, 2, 3, 4, 5, 6);
		//Returns a LongStream consisting of the elements of this stream, converted to long.
		LongStream  longStream1 = i.asLongStream();	
		longStream1.forEach(System.out::println);
		
		System.out.println("---------- Seconed Example -----------------");
		LongStream  longStream2  = IntStream.range(0, 7).mapToLong(n -> n);
		longStream2.forEach(System.out::println);
		System.out.println("---------------------------------------");
	}
}

Output:

---------- First Example -----------------
0
1
2
3
4
5
6
---------- Seconed Example -----------------
0
1
2
3
4
5
6
---------------------------------------

 

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.

*