Learn how to print array in java 8 using Java stream. Displaying of java.lang.Arrays using Arrray.stream() in Java programming language.
1. print array in Java 8
Well, display array using Java 8, there is multiple ways to print any type of array int/double/boolean/long or string array or any other type of array or custom array. In this post, I demonstrate by using the stream in java 8. And in the previous post know how to print in the normal way by using java.lang.Arrays
library class, then click on this link.
2. Example to display array using java 8
package com.mysoftkey.util; import java.util.Arrays; /** * @author ranjeet jha * */ public class PrintArrayInJava8Example { /** * @param args */ public static void main(String[] args) { System.out.println("\n Single dimensioal array of string, using stream..."); String[] arrayStr = new String[]{"ranjeet", "anila", "anushka", "tanisha"}; Arrays.stream(arrayStr).forEach(System.out::println); int[] arrayInt = {1, 3, 5, 4}; Arrays.stream(arrayInt).forEach(System.out::println); //2 Dimensional String array System.out.println("\n Deep/nested Arrays of String using stream..."); String[][] deepArrayStr = new String[][]{{"ranjeet", "Jha"}, {"Tanisha", "Anushka"}}; Arrays.stream(deepArrayStr).flatMap(x -> Arrays.stream(x)).forEach(System.out::println); System.out.println("\n Deep/nested Arrays using stream..."); int[][] deepArrayInt = new int[][]{{1, 5, 7, 9}, {2, 6, 8}}; Arrays.stream(deepArrayInt).flatMapToInt(x -> Arrays.stream(x)).forEach(System.out::println); } }
3. Program Output to print using Arrays.stream
Single dimensioal array of string, using stream... ranjeet anila anushka tanisha 1 3 5 4 Deep/nested Arrays of String using stream... ranjeet Jha Tanisha Anushka Deep/nested Arrays using stream... 1 5 7 9 2 6 8
5. Reference
I hope you enjoyed this post to print array in Java 8 using java.lang.Arrays.stream()
, you can visit visit Core Java tutorial for more blog post.
Happy Learning! 🙂
Please write your comments/suggestions to improve this post. Happy Learning! cheers 🙂
Pingback: Neeraj