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;
import java.util.stream.Collectors;

/*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);
List<Integer> oddNumbers = numbers.stream().
filter(o -> o % 2 != 0).
collect(Collectors.toList());
System.out.println(oddNumbers);
}
}

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

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/*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);
List<Integer> evenNumbers = numbers.stream().
filter(o -> o % 2 == 0).
collect(Collectors.toList());
System.out.println(evenNumbers);
}
}



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);
}
}

Example 5: Java 8 program to print odd numbers from a Set

import java.util.Set;
import java.util.stream.Collectors;

/*Java 8 Program to find Odd Numbers from a set*/
public class DriverClass {
public static void main(String[] args) {
Set<Integer> numbers = Set.of(1, 4, 8, 40, 11, 22, 33, 99);
Set<Integer> oddNumbers = numbers.stream().
filter(o -> o % 2 != 0).
collect(Collectors.toSet());
System.out.println(oddNumbers);
}
}


Example 6: Java 8 program to print even numbers from a Set

import java.util.Set;
import java.util.stream.Collectors;

/*Java 8 Program to find even numbers from a set*/
public class DriverClass {
public static void main(String[] args) {
Set<Integer> numbers =Set.of(1, 4, 8, 40, 11, 22, 33, 99);
Set<Integer> evenNumbers = numbers.stream().
filter(o -> o % 2 == 0).
collect(Collectors.toSet());
System.out.println(evenNumbers);
}
} 

Spring Boot | Azure

Spring Boot + React JS


Popular posts from this blog

Spring boot video streaming example-HTML5

Spring Boot + Mockito simple application with 100% code coverage

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

ReactJS, Spring Boot JWT Authentication Example

Spring Webflux File Download Example

Java, Spring Boot Mini Project - Library Management System - Download

DataTable-Pagination example with Spring boot, jQuery and ajax