Home >Java >javaTutorial >Java program to find maximum odd number in array using stream and filter

Java program to find maximum odd number in array using stream and filter

WBOY
WBOYforward
2023-09-09 19:49:021313browse

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete