Write a Java 8 program to print the first 10 odd numbers
Categories: Java 9 Java 8(JDK1.8) Java Java Examples
package r4r.co.in;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class First10thOdd {
//Write a Java 8 program to print the first 10 odd numbers.
public static void main(String[] args) {
List<Integer> list=Stream.iterate(0,i->i+1).filter(f->f%2==0).limit(10).collect(Collectors.toList());
System.out.println(list);
List<Integer> integers=Stream.iterate(0, i->i+2).limit(10).collect(Collectors.toList());
System.out.println(integers);
}
}
OutPut
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]