Wednesday, October 26, 2016

Problem No: #10 How to Print Only Some Elements in Integer Array using Stream?

Java 8 :: Streams and Lambda Expressions

/*
 * Problem No: #10
 * Problem Definition: How to Print Only Some Elements 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 StreamThreeEx {
public static void main(String[] args) {
int intArr[] = new int[15];

 intArr[0] = 11;
 intArr[1] = 21;
 intArr[2] = 31;
 intArr[3] = 41;
 intArr[4] = 51;
 intArr[5] = 61;
 intArr[6] = 71;
 intArr[7] = 81;
 intArr[8] = 91;
 intArr[9] = 21;
 intArr[10] = 22;
 intArr[11] = 23;
 intArr[12] = 24;
 intArr[13] = 25;
 intArr[14] = 26;
     
System.out.println("Printing All Elements of Stream: ");
 
Arrays.stream(intArr).forEach(System.out::println);
     
System.out.println("Printing Limited Elements of Stream: ");
 
Arrays.stream(intArr).limit(4).forEach(System.out::println);

}
}

No comments:

Post a Comment