Guava – Longs.min()方法与实例

来源:这里教程网 时间:2026-02-17 21:41:31 作者:

Guava – Longs.min() 方法与实例

Longs.min() 是Guava库中Longs类的一个方法,用于查找数组中存在的 least 值。该方法返回的值是指定数组中最小的长值。

语法:

public static long min(long... array)

参数: 这个方法需要一个强制参数 array ,它是一个非空的长值数组。

返回值: 本方法返回一个长值,是指定数组中的最小值。

异常情况: 如果数组为空,该方法会抛出IllegalArgumentException。

以下程序说明了上述方法的使用。

例1:

// Java code to show implementation of// Guava's Longs.min() method  import com.google.common.primitives.Longs;import java.util.Arrays;  class GFG {      // Driver's code    public static void main(String[] args)    {          // Creating a long array        long[] arr = { 2, 4, 6, 10, 0, -5, 15 };          // Using Longs.min() method to get the        // minimum value present in the array        System.out.println("Minimum Value is : "                           + Longs.min(arr));    }}

输出:

Minimum Value is : -5

例2:

// Java code to show implementation of// Guava's Longs.min() method  import com.google.common.primitives.Longs;import java.util.Arrays;  class GFG {      // Driver's code    public static void main(String[] args)    {          // Creating a long array        long[] arr = {};          try {            // Using Longs.min() method to get the            // minimum value present in the array            // This should raise "IllegalArgumentException"            // as the array is empty            System.out.println("Minimum Value is : "                               + Longs.min(arr));        }        catch (Exception e) {            System.out.println(e);        }    }}

输出:

java.lang.IllegalArgumentException

参考:

https://google.github.io/guava/releases/21.0/api/docs/com/google/common/primitives/Longs.html#min-long…-

相关推荐