Java Collections max()方法及实例
max(Collection<? extends T> coll)
java.util.Collections 类的 max() 方法是用来返回给定集合中的最大元素,根据其元素的自然排序。集合中的所有元素必须实现可比较接口。此外,集合中的所有元素必须是相互可比的(也就是说,e1.compareTo(e2)不能对集合中的任何元素e1和e2抛出ClassCastException)。
这个方法在整个集合中进行迭代,因此它需要的时间与集合的大小成比例。
语法
public static <T extends Object & Comparable> T
max(Collection coll)
参数: 该方法将集合 coll 作为参数,其最大元素将被确定。
返回值: 该方法根据其元素的自然排序,返回给定集合的 最大元素 。
异常: 该方法会抛出以下异常。
- ClassCastException – 如果集合中包含不能相互比较的元素(例如,字符串和整数)。
- NoSuchElementException – 如果集合是空的。
下面是说明 max() 方法的例子
例1 :
// Java program to demonstrate
// max() method for Integer value
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// creating object of LinkedList
List<Integer> list = new LinkedList<Integer>();
// Adding element to Vector v
list.add(-1);
list.add(4);
list.add(-5);
list.add(1);
// printing the max value
// using max() method
System.out.println("Max value is: "
+ Collections.max(list));
}
catch (ClassCastException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchElementException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出
Max value is: 4
例2: 对于 ClassCastException
// Java program to demonstrate
// max() method for ClassCastException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// creating object of LinkedList
List<String> list = new LinkedList<String>();
// creating variable of object type
Object i = Integer.valueOf(42);
// Adding element to Vector v
list.add("Hello");
list.add((String)i);
// printing the max value
// using max() method
System.out.println("Max value is: "
+ Collections.max(list));
}
catch (ClassCastException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchElementException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出
Exception thrown : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
例3: 对于NoSuchElementException
// Java program to demonstrate
// max() method for NoSuchElementException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// creating object of LinkedList
List<Integer> list = new LinkedList<Integer>();
// printing the max value
// using max() method
System.out.println("Trying to get "
+ "the max from empty list");
System.out.println("Max value is: "
+ Collections.max(list));
}
catch (ClassCastException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchElementException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出
Trying to get the max from empty list
Exception thrown : java.util.NoSuchElementException
public static T max(Collection<? extends T> coll, Comparator<? super T> comp)
java.util.Collections 类的 max() 方法用于返回给定集合中的最大元素,根据指定的比较器引起的顺序。集合中的所有元素必须通过指定的比较器进行相互比较(也就是说,comp.compare(e1, e2)不能对集合中的任何元素e1和e2抛出ClassCastException)。
这个方法在整个集合中进行迭代,因此它需要的时间与集合的大小成比例。
参数: 该方法接受以下参数作为参数
- coll – 要确定其最大元素的集合。
- comp – 用来确定最大元素的比较器。一个空值表示应该使用元素的自然排序。
返回值: 这个方法根据指定的比较器返回给定集合的 最大元素 。
异常: 该方法会抛出以下异常。
- ClassCastException – 如果集合中包含不能相互比较的元素(例如,字符串和整数)。
- NoSuchElementException – 如果集合是空的。
下面是说明 max() 方法的例子
例1 :
// Java program to demonstrate
// max() method for Integer value
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// creating object of LinkedList
List<Integer> list = new LinkedList<Integer>();
// Adding element to Vector v
list.add(-1);
list.add(4);
list.add(-5);
list.add(1);
// printing the max value
// using max() method
System.out.println("Max val: "
+ Collections.max(list,
Collections.reverseOrder()));
}
catch (ClassCastException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchElementException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出
Max val: -5
例2: 对于ClassCastException
// Java program to demonstrate
// max() method for ClassCastException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// creating object of LinkedList
List<String> list = new LinkedList<String>();
// creating variable of object type
Object i = Integer.valueOf(42);
// Adding element to Vector v
list.add("Hello");
list.add((String)i);
// printing the max value
// using max() method
System.out.println("Max val: "
+ Collections
.max(list,
Collections
.reverseOrder()));
}
catch (ClassCastException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchElementException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出
Exception thrown : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
例3: 对于NoSuchElementException
// Java program to demonstrate
// max() method for NoSuchElementException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// creating object of LinkedList
List<Integer> list = new LinkedList<Integer>();
// printing the max value
// using max() method
System.out.println("Trying to get "
+ "the max from empty list");
System.out.println("Max val: "
+ Collections
.max(list,
Collections
.reverseOrder()));
}
catch (ClassCastException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchElementException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出
Trying to get the max from empty list
Exception thrown : java.util.NoSuchElementException