Java Collections max()方法与示例

本文详细介绍了Java中Collections的max()方法,包括max()和max(Comparator)两个重载方法,用于返回集合中最大元素。这两个方法可能在返回最大值时抛出ClassCastException或NoSuchElementException异常。此外,max()方法是静态的,可以直接通过类名调用,而不会产生错误。文章还提供了一个示例来展示如何使用这两个方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

集合类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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值