Java Example – Finding the Maximum and Minimum Values in a List
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