Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Collections.max () to find the maximum of Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use Collections.max () to maximize Java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Rely on Collections.max () implementation

You can also find the maximum and minimum values using the Collections collection utility class, but before using it, we want to convert an array (Array) to a collection (List), as follows:

Import org.apache.commons.lang3.ArrayUtils

Import java.util.Arrays

Import java.util.Collections

Public class ArrayMax {

Public static void main (String [] args) {

Int [] arr = {3,7,2,1,-4}

Int max = findMaxByCollections (arr); / / find the maximum value based on Collections

System.out.println ("Max is:" + max)

}

/ * *

* find the maximum value based on Collections

* @ param arr Array to be queried

* @ return maximum

, /

Private static int findMaxByCollections (int [] arr) {

List list = Arrays.asList (

Org.apache.commons.lang3.ArrayUtils.toObject (arr))

Return Collections.max (list)

}

}

The results of the above procedures are as follows:

The maximum is: 7

The above is all the contents of the article "how to maximize the value of Java using Collections.max ()". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report