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...







Popular posts from this blog

Spring boot video streaming example-HTML5

DataTable-Pagination example with Spring boot, jQuery and ajax

10 Best Job Posting Sites 2021-2022

Spring boot web project free download:User Registration System

Spring Boot + JPA/Hibernate One to Many mapping example

5 Hardest Puzzle,100% fail answers

Java security AES,SHA256,SHA512,MD5-Spring Boot Project Free Download

Spring Boot-AngularJS-Bootstrap-JPA-CRUD

ReactJS - Bootstrap - Buttons

Spring Boot file upload/download example