The problem
Write a program that displays "Welcome to Java" five times.
Breaking it down
Java 8 IntStream
provides a way to generate a range of numbers in which we will iterate over using a Stream.foreach
method. Then creating a function that will print stream of strings will output "Welcome to java" five times. In addition to the IntStream.range
, Instream.rangeClosed
starts inclusive of first number and ends includes of the second parameter number.
public static void main(String[] args) {
IntStream.range(1, 5).forEach(i -> {
System.out.println("Welcome to Java");
});
}
Output
Welcome to Java
Welcome to Java
Welcome to Java
Welcome to Java