Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Finding the Maximum and Minimum Values in a List

Java Example – Finding the Maximum and Minimum Values in a List

Java Examples Java Examples

The following example demonstrates how to use the max() and min() methods of the Collections class to obtain the maximum and minimum values in a List:

Main.java File

import

java

.

util

.*;

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

{

List

list

=

Arrays

.

asList

(

"

one Two three Four five six one three Four

"

.

split

(

"

"

)

)

;

System

.

out

.

println

(

list

)

;

System

.

out

.

println

(

"

Maximum value:

"

Collections

.

max

(

list

)

)

;

System

.

out

.

println

(

"

Minimum value:

"

Collections

.

min

(

list

)

)

;

}

}

The output of the above code is:



[one, Two, three, Four, five, six, one, three, Four]

最大值: three

最小值: Four

Java Examples Java Examples