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.
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:
Largest odd number: 51
Using 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:
Largest odd number: 51
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Stream filter() in Java with examples
- Sum of list with stream filter in Java
- Difference between Stream.of() and Arrays.stream() method in Java
- Character Stream Vs Byte Stream in Java
- foreach() loop vs Stream foreach() vs Parallel Stream foreach()
- Program to convert a Set to Stream in Java using Generics
- Program to Convert Stream to an Array in Java
- Program to convert Boxed Array to Stream in Java
- Program to convert Primitive Array to Stream in Java
- Java Program to Find the Number Occurring Odd Number of Times
- Program to Convert List to Stream in Java
- Program to convert a Map to a Stream in Java
- Program to Iterate over a Stream with Indices in Java 8
- How to find duplicate elements in a Stream in Java
- Find the last element of a Stream in Java
- Find the first element of a Stream in Java
- Flatten a Stream of Lists in Java using forEach loop
- Flatten a Stream of Arrays in Java using forEach loop
- Flatten a Stream of Map in Java using forEach loop
- Count occurrence of a given character in a string using Stream API in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.