Learn Java 8 streams with an example - print odd/even numbers from array and List

Example 1: Java 8 program to print odd numbers from a List

import java.util.Arrays;
import java.util.List;

/*Java 8 Program to find Odd Numbers from a list*/
public class DriverClass {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 4, 8, 40, 11, 22, 33, 99);
numbers.stream().filter(o -> o % 2 != 0).forEach(System.out::println);
}
}


Example 2: Java 8 program to print even numbers from a List

import java.util.Arrays;
import java.util.List;

/*Java 8 Program to find Even Numbers from a list*/
public class DriverClass {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 4, 8, 40, 11, 22, 33, 99);
numbers.stream().filter(o -> o % 2 == 0).forEach(System.out::println);
}
}


Example 3: Java 8 program to print even numbers from an array

import java.util.Arrays;

/*Java 8 Program to find Even Numbers from an Array*/
public class DriverClass {
public static void main(String[] args) {
int[] numbers = {2, 5, 7, 8, 99, 1, 22, 4, 3, 77, 66};
Arrays.stream(numbers).filter(o -> o % 2 == 0).forEach(System.out::println);
}
}


Example 4: Java 8 program to print odd numbers from an array

import java.util.Arrays;

/*Java 8 Program to find Odd Numbers from an Array*/
public class DriverClass {
public static void main(String[] args) {
int[] numbers = {2, 5, 7, 8, 99, 1, 22, 4, 3, 77, 66};
Arrays.stream(numbers).filter(o -> o % 2 != 0).forEach(System.out::println);
}
}


More...







Comments