Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

In Java8 – How to Convert Array to Stream using Arrays.stream() and Stream.of() Operations

$
0
0

Java8 - How to convert Array to Stream - Crunchify Tips

Java8 is pretty amazing. With lots of new features and Stream APIs Java8 is one of the best release we had last year.

In this tutorial we will go over how to convert Array to Stream using Java8’s Arrays.stream and Stream.of operations.

Here are the steps:

  1. Create Object Array crunchifyCompany
    1. Use Arrays.stream() and
    2. Stream.of() utility to covert Array to Stream
  2. Create Primitive Array doubleCrunchify
    1. Use Arrays.stream() and
    2. Stream.of() -> flatMapToDouble() to covert Array to Stream

java.util.stream.Stream.flatMapToDouble - Java8 Crunchify Tips

Java Code:

package crunchify.com.tutorial;

import java.util.Arrays;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyArrayToStreamInJava8 {
	public static void main(String[] args) {

		// Try with Object Array
		String[] crunchifyCompany = { "Twitter", "Facebook", "Yahoo", "Google" };

		// Arrays.stream - returns a sequential Stream with the specified array as its source
		Stream<String> crunchifyStream = Arrays.stream(crunchifyCompany);
		log("\n1. Arrays.stream output for Object Array:");
		crunchifyStream.forEach(company -> log(company));

		// Stream.of - returns a sequential ordered stream whose elements are the specified values
		Stream<String> crunchifyStream2 = Stream.of(crunchifyCompany);
		log("\n2. Stream.of output for Object Array:");
		crunchifyStream2.forEach(company -> log(company));

		// Now try with Primitive Arrays
		double[] doubleCrunchify = { 11.1, 21.2, 31.3, 41.4, 51.5 };

		DoubleStream doubleStream = Arrays.stream(doubleCrunchify);
		log("\n1. Arrays.stream output for Primitive Arrays:");
		doubleStream.forEach(company -> System.out.println(company));

		Stream<double[]> crunchify = Stream.of(doubleCrunchify);
		// flatMapToDouble - 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 doubleStream2 = crunchify.flatMapToDouble(doubleArray -> Arrays.stream(doubleArray));
		log("\n2. Stream.of output for Primitive Arrays:");
		doubleStream2.forEach(company -> System.out.println(company));
	}

	private static void log(String crunchifyString) {
		System.out.println(crunchifyString);

	}
}

Result:

1. Arrays.stream output for Object Array:
Twitter
Facebook
Yahoo
Google

2. Stream.of output for Object Array:
Twitter
Facebook
Yahoo
Google

1. Arrays.stream output for Primitive Arrays:
11.1
21.2
31.3
41.4
51.5

2. Stream.of output for Primitive Arrays:
11.1
21.2
31.3
41.4
51.5

Have a suggestion or anything to add to this article? Chime in and share as a comment.

The post In Java8 – How to Convert Array to Stream using Arrays.stream() and Stream.of() Operations appeared first on Crunchify.
Author: App Shah


Viewing all articles
Browse latest Browse all 1037

Trending Articles