
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Maximum Odd Number in Array Using Stream and Filter in Java
In this section, we are going to write a Java program to find maximum odd number in an Array using Stream and Filter. Odd numbers are the numbers which cannot be divided by 2 or these numbers give remainder as 1 when they are divided by 2. In other terms which can be written in the form of 2n+1. We will find the maximum odd number in the array.
Problem Statement
Write a program in Java to find maximum odd number in array using stream and filter ?
Inputarray = {1, 7, 2, 3, 9, 5, 10}Output
Maximum odd number is 9
From the above example, in the array the maximum odd number is 9.
Methods Used
stream() ? It is used to create stream of elements so that we can use methods like filter(), map(), reduce() to process data
Arrays.stream(collection)
filter() ? It is used to filter data from stream i.e., to select specific elements from stream based on a condition. It returns Boolean value.
Streamobject.filter(condition)
reduce() ? It is used to reduce number of elements and return single result number based on the binary operation.
Streamobject.reduce(initial value, binary operation)
We will now discuss the different approaches for finding maximum odd number in an array using stream and filter using code implementation in Java.
Approach 1: Using stream() and filter() with max()
Below are the steps to find maximum odd number in array using stream() and filter() with max() method ?
-
Initialize an array and create a stream for the array using stream() method.
-
Filter the stream using filter method() and parameter as condition to filter to odd numbers from array.
-
Use max() method to return maximum odd number else print -1 using orElse() method if there are no odd numbers.
Example
import java.util.*; public class Main { public static void main(String[] args) { int[] array = {1, 7, 2, 3, 9, 5, 10}; int maximumOdd = Arrays.stream(array) .filter(n -> n % 2 != 0) .max() .orElse(-1); System.out.println("Maximum odd number is: " +maximumOdd); } }
Output
Maximum odd number is: 9
Code Explanation
In the above example, we initially initialize an array. Then we use stream() method to convert array to stream and then use filter() method on the stream to filter out the odd numbers present in the stream and on the resultant stream we use the max() method to find the maximum of all odd numbers in the stream. If there is no odd number present in the stream, then we use orElse function which returns value of input parameter. Then we print the value stored in the maximumOdd variable.
Approach 2: Using stream(), filter() with reduce() methods
Below are the steps to find maximum odd number in array using stream(), filter() with reduce() methods ?
-
Initialize an array and create a stream for the array using stream() method
-
Filter the stream using filter() method and parameter as condition to filter to odd numbers from array.
-
Using reduce() method find the maximum odd number.
-
Using ternary operator print the maximum odd number or else print -1 if there is no odd number.
Example
import java.util.*; public class Main { public static void main(String[] args) { int[] array = {1, 7, 2, 3, 9, 5}; int maximumOdd = Arrays.stream(array) .filter(n -> n % 2 != 0) .reduce(Integer.MIN_VALUE, Integer::max); System.out.println("Maximum odd number in the given array is " + (maximumOdd != Integer.MIN_VALUE ? maximumOdd : -1)); } }
Output
Maximum odd number in the given array is 9
Code Explanation
In this example, we initially initialize an array. Then we use stream() method to convert array to stream and then use filter() method on the stream to filter out the odd numbers present in the stream and on the resultant stream we use the reduce() method to find the maximum of all odd numbers in the stream. If there is no odd number present in the stream, then the maximumOdd number contains Integer.MIN_VALUE. We then use ternary operation ? and check whether the maximumOdd variable contains Integer.MIN_VALUE or not. If it contains Integer.MIN_VALUE then we print -1 else we print the stored value in maximumOdd variable.
Thus, in this article we have discussed how to find maximum odd number in an array using Stream and Filter in Java using different approaches.