Java Program to Find Maximum Odd Number in Array Using Stream and Filter Last Updated : 29 Oct, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Java 8 introduced some great features like Stream and Filter which tremendously simplify tasks like reading data and performing operations like minimum, maximum, sum, and conditional check on them. In this program, we will get the maximum of all odd numbers from a list of integers with the help of the Java Stream and Filter method. Using Loops In the absence of Streams, we could achieve the given task by iterating through the list and checking if the number is odd. If true, we would check if it is larger than the maximum odd number until now. Below is the implementation of the approach: Java // Java implementation to find // the maximum odd number in array import java.util.List; import java.util.ArrayList; import java.util.Iterator; class GFG { // Function to find the maximum // odd number in array public static int maxOdd(List<Integer> list) { // Iterator for accessing the elements Iterator<Integer> it = list.iterator(); int max = 0; while (it.hasNext()) { int num = it.next(); // Adding the elements // greater than 5 if (num % 2 == 1) { if (num > max) { max = num; } } } return max; } // Driver Code public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(11); list.add(43); list.add(56); list.add(82); list.add(51); list.add(29); list.add(10); System.out.println("Largest odd number: " + maxOdd(list)); } } OutputLargest odd number: 51Using Stream and Filter Stream filter returns a stream consisting of the elements of this stream that match the given predicate. This is an intermediate operation. These operations are always lazy i.e, executing an intermediate operation such as filter() does not actually perform any filtering, but instead creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate. Below is the implementation of the above approach: Java // Java implementation to find // the maximum odd number // in the array import java.util.List; import java.util.ArrayList; import java.util.Iterator; class GFG { // Function to find the maximum odd // number in the list public static int maxOdd(List<Integer> list) { // Converted to Stream // Filtering the odd number // Taking maximum out of those integers return list.stream() .filter(n -> n % 2 == 1) .max(Integer::compare) .orElse(0); } // Driver Code public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(11); list.add(43); list.add(56); list.add(82); list.add(51); list.add(29); list.add(10); System.out.println("Largest odd number: " + maxOdd(list)); } } OutputLargest odd number: 51 Performance Analysis: Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Java Program to Find the K'th largest element in a stream S SAKSHIKULSHRESHTHA Follow Improve Article Tags : Java Java Programs Java-Array-Programs Practice Tags : Java Similar Reads Java Program to Find Largest Element in an Array Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java.Example Input/Output:Input: arr = { 1, 2, 3, 4, 5}Output: 5Input: arr = { 10, 3, 5, 7, 2, 12}Ou 4 min read Java Program to Find the K'th largest element in a stream Given an infinite stream of integers, find the k'th largest element at any point of time.Example: Input: stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...} k = 3 Output: {_, _, 10, 11, 20, 40, 50, 50, ...} Extra space allowed is O(k). Recommended: Please solve it on "PRACTICE" first, before moving on 4 min read How to Perform Parallel Processing on Arrays in Java Using Streams? In Java, parallel processing helps to increase overall performance. Arrays may be processed in parallel in Java by using Streams API. When working with big datasets or computationally demanding tasks, this is very helpful. In this article, we will learn how to perform parallel processing on arrays i 2 min read Java Program to Store Even & Odd Elements of an Array into Separate Arrays Given an array with N numbers and separate those numbers into two arrays by odd numbers or even numbers. The complete operation required O(n) time complexity in the best case. For optimizing the memory uses, the first traverse through an array and calculate the total number of even and odd numbers i 2 min read How to Find the Maximum Element in an Array? In Java, the array is a data structure that allows the users to store data of the same type in contiguous memory locations. To find the maximum element in an Array in Java, we can sort the array in ascending order using the Arrays.sort() method and then we can access the last element of the array wh 1 min read Implement Filter Function using Reduce in Java 8 Streams Many times, we need to perform operations where a stream reduces to a single resultant value, for example, maximum, minimum, sum, product, etc. Reducing is the repeated process of combining all elements. reduce operation applies a binary operator to each element in the stream where the first argumen 2 min read Like