Java program to find maximum odd number in array using stream and filter
In this section, we will write a Java program to find the largest odd number in an array using streams and filters. Odd numbers are numbers that are not divisible by "2", or the remainder is 1 when divided by "2". In other words, it can be written in the form ‘2n 1’. We will find the largest odd number in the array.
Example
Input: array = {1, 7, 2, 3, 9, 5, 10} Output: Maximum odd number is 9
From the above example, the largest odd number in the array is 9.
Input: array = {11, 17, 12, 13, 19, 15, 20} Output: Maximum odd number is 19
From the above example, the largest odd number in the array is 19.
usage instructions
stream() - It is used to create a stream of elements so that we can use methods such as filter(), map(), reduce(), etc. to process the data
Arrays.stream(collection)
filter() - Used to filter data in a stream, i.e. select specific elements from the stream based on conditions. It returns a boolean value.
treamobject.filter(condition)
reduce() - Used to reduce the number of elements and return a single result number based on a binary operation.
Streamobject.reduce(initial value, binary operation)
We will now discuss different ways to find the largest odd number in an array using streams and filters (implemented using code in Java).
algorithm
Initialize the array and use the stream() method to create a stream for the array
Use filter method() and parameters as conditions to filter the stream to filter out odd numbers from the array.
Use the max() method to return the largest odd number. If there is no odd number, use the orElse() method to print -1.
Example
In this example, we first initialize an array. Then we use the "stream()" method to convert the array into a stream, then use the "filter()" method on the stream to filter out the odd numbers present in the stream, and on the resulting stream we use the max() method to find all odd numbers in the stream the maximum value. If there are no odd numbers in the stream, we use the "orElse" function to return the value of the 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
Use stream(), filter() and reduce() methods
Initialize the array and use the stream() method to create a stream for the array
Use filter method() and parameters as conditions to filter the stream to filter out odd numbers from the array.
Use the reduce() method to find the largest odd number
Use the ternary operator to print the largest odd number, or -1 if there is no odd number.
Example
In this example, we first initialize an array. Then we use the "stream()" method to convert the array into a stream, then use the "filter()" method on the stream to filter out the odd numbers present in the stream, and on the resulting stream we use the reduce() method to find all odd numbers in the stream the maximum value. If there are no odd numbers in the stream, the MaximumOdd number contains Integer.MIN_VALUE. We then use the ternary operation ‘?’ and check if the ‘maximumOdd’ variable contains Integer.MIN_VALUE. If it contains Integer.MIN_VALUE, then we print -1, otherwise 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}; 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
So, in this article, we have discussed how to find the maximum odd number in an array using different methods using streams and filters in Java.
The above is the detailed content of Java program to find maximum odd number in array using stream and filter. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool