How to print array in java 8

Connect with

java8_logo Well, there are multiple way to print any type of array int/double/boolean/long or string array or any another type of array or custom array. In this post I demonstrate by using stream in java 8. And in the previous post , by using Arrays utility class , in you want to know how to print in normal way by using java.lang.Arrays library class, then click on this link.

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);

 }

}

Program Output

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

Please write you comments/suggest to improve this post. cheers 🙂


Connect with

1 thought on “How to print array in java 8

  1. Pingback: Neeraj

Leave a Reply

Your email address will not be published. Required fields are marked *