集合类max()方法 (Collections Class max() method)
Syntax:
句法:
public static Type max(Collection co);
public static Type max(Collection co, Comparator com);
max() method is available in java.util package.
max()方法在java.util包中可用。
max(Collection co) method is used to return the greatest value element of the given collection depend on natural sorting.
max(Collection co)方法用于根据自然排序返回给定集合的最大值元素。
max(Collection co, Comparator com) method is used to return the greatest value element of the given collection depend on customizing sorting as given Comparator object.
max(Collection co,Comparator com)方法用于返回给定集合的最大值元素,该元素取决于根据给定Comparator对象自定义排序。
These methods may throw an exception at the time of returning the maximum element.
这些方法在返回最大元素时可能会引发异常。
- ClassCastException: This exception may throw when the given collection elements are mutually incomparable.ClassCastException :当给定的collection元素相互不可比时,可能会抛出此异常。
- NoSuchElementException: This exception may throw when the given collection is "blank" (i.e. no elements).NoSuchElementException :当给定的集合为“空白”(即没有元素)时,可能会抛出此异常。
These are static methods and it is accessible with the class name and if we try to access these methods with the class object then also we will not get any error.
这些是静态方法,可以使用类名进行访问,如果尝试使用类对象访问这些方法,则也不会出现任何错误。
Parameter(s):
参数:
In the first case, max(Collection co),
在第一种情况下, max(Collection co) ,
- Collection co – represents the collection object whose greatest value element of the given collection object.
- 集合co –表示集合对象,其给定集合对象的最大价值元素。
In the first case, max(Collection co, Comparator com),
在第一种情况下, max(Collection co,Comparator com) ,
- Collection co – represents the collection object whose greatest value element of the given collection object.
- 集合co –表示集合对象,其给定集合对象的最大价值元素。
- Comparator com – represents the Comparator with which to calculate the maximum element.
- 比较器com –表示用来计算最大元素的比较器。
Return value:
返回值:
In both the cases, the return type of the method is Type, it returns the greatest value element of the given collection depend upon the given Comparator.
在这两种情况下,方法的返回类型都是Type ,它根据给定的Comparator返回给定集合的最大值元素。
Example:
例:
// Java program to demonstrate the example
// of max() method of Collections
import java.util.*;
public class MaxOfCollections {
public static void main(String args[]) {
// Instantiates an ArrayList
ArrayList arr_l = new ArrayList();
// By using add() method is to add
// objects in an array list
arr_l.add(20);
arr_l.add(10);
arr_l.add(50);
arr_l.add(40);
arr_l.add(80);
// Display ArrayList
System.out.println("arr_l: " + arr_l);
// By using max(arr_l,Comparator) method is
// to return the maximum element based on the
// defined comparator object and here we set null
// that means comparator follows default ordering
System.out.print("Collections.max(arr_l,null): ");
System.out.print(Collections.max(arr_l, null));
System.out.println();
// By using max(arr_l) method is
// to return the maximum element based on the
// natural order without using comparator object
System.out.print("Collections.max(arr_l): ");
System.out.print(Collections.max(arr_l));
}
}
Output
输出量
arr_l: [20, 10, 50, 40, 80]
Collections.max(arr_l,null): 80
Collections.max(arr_l): 80
翻译自: https://www.includehelp.com/java/collections-max-method-with-example.aspx