Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-01 03:06:19 09月18日 Update

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!

Tags: Maximum maximum article content array minimum not so much code most tools more knowledge procedures results industry information information channels channels references learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi vpn macOS Microsoft