search
HomeJavajavaTutorialJava 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

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. If there is any infringement, please contact admin@php.cn delete
Vue3中的过滤器函数:优雅的处理数据Vue3中的过滤器函数:优雅的处理数据Jun 18, 2023 pm 02:46 PM

Vue3中的过滤器函数:优雅的处理数据Vue是一个流行的JavaScript框架,拥有庞大的社区和强大的插件系统。在Vue中,过滤器函数是一种非常实用的工具,允许我们在模板中对数据进行处理和格式化。Vue3中的过滤器函数有了一些改变,在这篇文章中,我们将深入探讨Vue3中的过滤器函数,学习如何使用它们优雅地处理数据。什么是过滤器函数?在Vue中,过滤器函数是

Vue 中使用插件实现自定义过滤器的技巧Vue 中使用插件实现自定义过滤器的技巧Jun 25, 2023 pm 05:01 PM

Vue中使用插件实现自定义过滤器的技巧Vue.js提供了一种方便的方式来处理视图数据过滤的需求,即过滤器(Filter)。过滤器主要负责将视图中的数据进行格式化和处理,使数据更加直观和易于理解。Vue内置了一些常用的过滤器,例如日期格式化、货币格式化等,同时也支持自定义过滤器。本文将介绍如何使用Vue插件实现自定义过滤器的技巧,并提供一些实用的过滤

Vue技术开发中如何进行数据筛选和排序Vue技术开发中如何进行数据筛选和排序Oct 09, 2023 pm 01:25 PM

Vue技术开发中如何进行数据筛选和排序在Vue技术开发中,数据筛选和排序是非常常见和重要的功能。通过数据筛选和排序,我们可以快速查询和展示我们需要的信息,提高用户体验。本文将介绍在Vue中如何进行数据筛选和排序,并提供具体的代码示例,帮助读者更好地理解和运用这些功能。一、数据筛选数据筛选是指根据特定的条件筛选出符合要求的数据。在Vue中,我们可以通过comp

Vue报错:无法正确使用filters中的过滤器,怎样解决?Vue报错:无法正确使用filters中的过滤器,怎样解决?Aug 26, 2023 pm 01:10 PM

Vue报错:无法正确使用filters中的过滤器,怎样解决?引言:在Vue中,过滤器(filters)是一个常用的功能,可以用来对数据进行格式化或者过滤。然而,在使用过程中,有时候我们可能会遇到无法正确使用过滤器的问题。本文将介绍一些常见的原因和解决方法。一、原因分析:过滤器未正确注册:Vue中的过滤器需要先进行注册,才能在模板中使用。如果过滤器未成功注册,

PHP电子邮件过滤器:过滤和识别垃圾邮件。PHP电子邮件过滤器:过滤和识别垃圾邮件。Sep 19, 2023 pm 12:51 PM

PHP电子邮件过滤器:过滤和识别垃圾邮件。随着电子邮件的广泛应用,垃圾邮件的数量也不断增加。对于用户来说,接收到的大量垃圾邮件会导致信息过载和时间浪费。因此,我们需要一种高效的方法来过滤和识别垃圾邮件。本文将介绍如何使用PHP编写一个简单但有效的电子邮件过滤器,并提供具体的代码示例。邮件过滤器基本原理邮件过滤器的基本原理是通过分析邮件的内容和属性,判断其是否

解决Tomcat乱码的几种方法解决Tomcat乱码的几种方法Dec 28, 2023 pm 01:32 PM

解决Tomcat中文乱码问题的几种方法,需要具体代码示例在Web开发中,我们经常会遇到Tomcat中文乱码问题。这种问题在处理中文字符时会导致乱码或者显示为方框、问号等字符,给用户带来不好的体验。为了解决这个问题,本文将介绍几种常用的方法,并提供具体的代码示例。修改Tomcat配置文件在Tomcat的安装目录下找到conf/server.xml文件,搜索默认

在PHP中,FILTER_VALIDATE_URL常量表示用于验证URL的过滤器在PHP中,FILTER_VALIDATE_URL常量表示用于验证URL的过滤器Sep 14, 2023 am 10:37 AM

FILTER_VALIDATE_URL常量用于验证URL。标志FILTER_FLAG_SCHEME_REQUIRED−URL必须符合RFC标准。FILTER_FLAG_HOST_REQUIRED−URL必须包含主机名。FILTER_FLAG_PATH_REQUIRED−URL必须在域名后面有路径。FILTER_FLAG_QUERY_REQUIRED−URL必须有查询字符串。返回值FILTER_VALIDATE_URL

Zend Framework中间件:实现邮件发送和通知功能Zend Framework中间件:实现邮件发送和通知功能Jul 31, 2023 pm 01:15 PM

ZendFramework是一种流行的PHP开发框架,它提供了丰富的功能和组件,使开发者能够快速构建可扩展的Web应用程序。其中一个强大的特性是中间件(Middleware),它可以用于实现各种功能,包括邮件发送和通知。本文将演示如何使用ZendFramework的中间件来实现邮件发送和通知功能。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool