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

1. Initialize List using Arrays.asList() method. Java's built-in Arrays.asList() method converts an array into a list. This method takes an array as an argument and returns a List object.

2. Java List interface provides stream() method which returns a sequential Stream with list of Integer as its source.

3. The Java stream filter() method allows us to narrow down the stream elements based on a criterion. Here we separated the odd numbers from all other numbers.

4. Using collect() method we put all odd numbers to a list.


Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example - click here 


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

1. Initialize List using Arrays.asList() method. Java's built-in Arrays.asList() method converts an array into a list. This method takes an array as an argument and returns a List object.

2. Java List interface provides stream() method which returns a sequential Stream with list of Integer as its source.

3. The Java stream filter() method allows us to narrow down the stream elements based on a criterion. Here we separated the even numbers from all other numbers.

4. Using collect() method we put all even numbers to a list.



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

1. Arrays.stream() is used to get a sequential stream from the array passed as the parameter with its elements.

2. The Java stream filter() method allows us to narrow down the stream elements based on a criterion. Here we separated the even numbers from all other numbers.

3. forEach() is a terminal operation, that performs an action for each element of the stream. Here printing each element.


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

1. Arrays.stream() is used to get a sequential stream from the array passed as the parameter with its elements.

2. The Java stream filter() method allows us to narrow down the stream elements based on a criterion. Here we separated the odd numbers from all other numbers.

3. forEach() is a terminal operation, that performs an action for each element of the stream. Here printing each element.


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

1. Created Immutable Set using Set.of() method

2. Java Set interface provides stream() method which returns a sequential Stream with set of Integer as its source.

3. The Java stream filter() method allows us to narrow down the stream elements based on a criterion. Here we separated the odd numbers from all other numbers.

4. Using collect() method we put all odd numbers to a Set.


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

1. Created Immutable Set using Set.of() method

2. Java Set interface provides stream() method which returns a sequential Stream with set of Integer as its source.

3. The Java stream filter() method allows us to narrow down the stream elements based on a criterion. Here we separated the even numbers from all other numbers.

4. Using collect() method we put all even numbers to a Set.


More interesting topics,

Spring Boot | GCP 

Test Slices in Spring-Boot


Integration Testing

Popular posts from this blog

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

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

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete