Wednesday, October 26, 2016

Problem No: #8 How to Print First Element present in Integer Array using Stream?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #8
 * Problem Definition: How to Print First Element present in Integer Array using Stream?
 * 
 * Answer: By Using, 
 * > java.util.Collection.stream(): Returns a sequential Stream with this collection as its source. 
 *  
 *> java.util.stream.Stream.forEach(): Performs an action for each element of this stream.This is a "terminal operation". 
 * 
 * **Same Operations can be applied with LongStream and DoubleStream
 * Program By: Mr.DIpak SOnar
 * Date : 26th Oct 2016
 */

import java.util.Arrays;

public class StreamOneEx {

public static void main(String[] args) {
int intArr[] = new int[7];

 intArr[0] = 11;
 intArr[1] = 21;
 intArr[2] = 31;
 intArr[3] = 41;
 intArr[4] = 51;
 intArr[5] = 61;
 intArr[6] = 71;

 // Arrays.stream(intArr).forEach(System.out::println);
// To Print Integer Array using Stream
 
 System.out.print("Printing First Element in Stream use findFirst():");
 System.out.println(Arrays.stream(intArr).findFirst().getAsInt());
 
}
}

No comments:

Post a Comment