Friday 11, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 2 July 2017

Java 8 - Convert Array to Stream

In this article we are going to see about Java 8 - Convert Array to Stream Example

ArrayToStream.java

01.package com.javatutorialcorner.java8;
02. 
03.import java.util.Arrays;
04.import java.util.stream.IntStream;
05.import java.util.stream.Stream;
06. 
07.public class ArrayToStream {
08. 
09. public static void main(String[] args) {
10.   String[] languages = {"Java", "C++", "Spring", "Hibernate", "R Language"};
11. 
12.         //Arrays.stream
13.         Stream<string> arraysStream = Arrays.stream(languages);
14.         arraysStream.forEach(x -> System.out.println(x));
15. 
16.         //Stream.of
17.         Stream<string> streamOf = Stream.of(languages);
18.         streamOf.forEach(x -> System.out.println(x));
19.          
20.         //For primitive array, the Arrays.stream and Stream.of will return different output.
21.         int[] intArray = {1, 2, 3, 4, 5};
22. 
23.         // 1. Arrays.stream -> IntStream
24.         IntStream intArraysStream = Arrays.stream(intArray);
25.         intArraysStream.forEach(x -> System.out.println(x));
26. 
27.         // 2. Stream.of-> Stream<int>
28.         Stream<int> intStreamOf = Stream.of(intArray);
29. 
30.         // Cant print Stream<int> directly, convert / flat it to IntStream
31.         IntStream intStream2 = intStreamOf.flatMapToInt(x -> Arrays.stream(x));
32.         intStream2.forEach(x -> System.out.println(x));
33. 
34. }
35. 
36.}
37.</int></int></int></string></string>

Output
Java
C++
Spring
Hibernate
R Language
Java
C++
Spring
Hibernate
R Language
1
2
3
4
5
1
2
3
4
5

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to Convert JSON String to Object using GSONIn this Tutorials we are going to see how to convert JSON String to Java object using GSON library. 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.gson  3. Create java class ca… Read More
  • Sending Email with AttachmentIn this tutorials we are going to see how to send the email with attachment using java program.I used gmail SMTP server for sending email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package … Read More
  • Sending Simple Email Using Java Mail APIIn this tutorials we are going to see how to send the simple email using java program.I used gmail SMTP server for sending email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package used for … Read More
  • How to Convert String to InputStreamIn this tutorials we are going to see how to convert String to InputStream using methods available in Java IO. BufferedReader is used to get String again from InputStream. Create project called JavaMisc. Create package ca… Read More
  • How to Create JSON Data using JSONSimple JSON – JavaScript Object Notation JSON is a simple data exchange format for read and write data.JSON is the best alternative for XML data.We can create JSON data using Jackson, Google Gson, JSON.simple libraries. In this … Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java 8 - Convert Array to Stream Rating: 5 Reviewed By: eHowToNow