Java中的Collections max()方法及示例
max(Collection<? extends T> coll)
java.util.Collections类的 max() 方法用于返回给定集合的最大元素,根据元素的自然排序。集合中的所有元素都必须实现Comparable接口。此外,集合中的所有元素必须可比较(也就是说,对集合中的任何元素e1和e2进行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);
}
}
}
输出:
异常抛出:java.lang.ClassCastException: java.lang.Integer无法转换为java.lang.String
示例3: 抛出 NoSuchElementException
//Java程序演示了NoSuchElementException的max()方法
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
//创建LinkedList对象
List list = new LinkedList();
//使用max()方法打印出最大值
System.out.println("尝试从空列表中获取最大值");
System.out.println("最大值是:" + Collections.max(list));
}
catch (ClassCastException e) {
System.out.println("异常抛出:" + e);
}
catch (NoSuchElementException e) {
System.out.println("异常抛出:" + e);
}
}
}
输出结果:
尝试从空列表中获取最大值
异常抛出:java.util.NoSuchElementException
public static T max(Collection coll, Comparator comp)
java.util.Collections类的 max() 方法用于根据指定的比较器所引出的顺序返回给定集合中的最大元素。集合中的所有元素必须通过指定的比较器进行相互比较(即,对于集合中的任何元素e1和e2,comp.compare(e1,e2)不能抛出ClassCastException)。
该方法对整个集合进行迭代,因此它需要与集合大小成比例的时间。
参数: 该方法将参数作为以下参数
- coll – 要确定其最大元素的集合。
- comp – 用于确定最大元素的比较器。null值表示应使用元素的自然排序。
返回值: 返回给定集合中经指定比较器确定的最大元素。
异常: 该方法抛出以下异常
- ClassCastException – 如果集合包含没有相互比较的元素(例如字符串和整数)。
- NoSuchElementException – 如果集合为空。
下面是说明 max() 方法的示例
示例1:
// 演示Integer值的max()方法的Java程序
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// 创建LinkedList对象
List<Integer> list = new LinkedList<Integer>();
// 将元素添加到向量v中
list.add(-1);
list.add(4);
list.add(-5);
list.add(1);
//使用max()方法打印最大值
System.out.println("最大值:"
+ Collections.max(list,
Collections.reverseOrder()));
}
catch (ClassCastException e) {
System.out.println("抛出异常:" + e);
}
catch (NoSuchElementException e) {
System.out.println("抛出异常:" + e);
}
}
}
输出:
最大值:-5
示例2: 用于ClassCastException
// 演示ClassCastException的max()方法的Java程序
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// 创建LinkedList对象
List<String> list = new LinkedList<String>();
// 创建对象类型变量
Object i = Integer.valueOf(42);
// 将元素添加到向量v中
list.add("Hello");
list.add((String)i);
//使用max()方法打印最大值
System.out.println("最大值:"
+ Collections
.max(list,
Collections
.reverseOrder()));
}
catch (ClassCastException e) {
System.out.println("抛出异常:" + e);
}
catch (NoSuchElementException e) {
System.out.println("抛出异常:" + e);
}
}
}
输出:
抛出异常:java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
示例3: 用于NoSuchElementException
// 演示NoSuchElementException的max()方法的Java程序
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// 创建LinkedList对象
List<Integer> list = new LinkedList<Integer>();
//使用max()方法打印最大值
System.out.println("尝试从空列表中获取最大值");
System.out.println("最大值:"
+ Collections
.max(list,
Collections
.reverseOrder()));
}
catch (ClassCastException e) {
System.out.println("抛出异常:" + e);
}
catch (NoSuchElementException e) {
System.out.println("抛出异常:" + e);
}
}
}
输出:
尝试从空列表中获取最大值
抛出异常:java.util.NoSuchElementException