Java 8 :: Streams and Lambda Expressions
/*
* Problem No: #12
* Problem Definition: How to Print Limited amount of Random Numbers 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.Random;
public class StreamsWithRandomEx {
public static void main(String[] args) {
Random random = new Random();
// unlimited random numbers get generated
// random.ints().forEach(System.out::println);
random.ints().limit(10).forEach(System.out::println);
}
}
/*
* Problem No: #12
* Problem Definition: How to Print Limited amount of Random Numbers 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.Random;
public class StreamsWithRandomEx {
public static void main(String[] args) {
Random random = new Random();
// unlimited random numbers get generated
// random.ints().forEach(System.out::println);
random.ints().limit(10).forEach(System.out::println);
}
}
No comments:
Post a Comment