- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Find Maximum Odd Number in Array Using Stream and Filter
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.
Examples
Input: array = {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.
Input: array = {11, 17, 12, 13, 19, 15, 20} Output: Maximum odd number is 19
From the above example, in the array the maximum odd number is 19.
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.
treamobject.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.
Algorithm
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
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 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.
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
Using stream(), filter() and 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
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.
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
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.