当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Guava Longs.min()用法及代码示例


Longs.min()是Guava库中Longs Class的一种方法,用于查找数组中存在的最小值。此方法返回的值是指定数组中最小的long值。

用法:

public static long min(long... array)

参数:此方法采用强制参数数组,该参数数组是long值的非空数组。


返回值:此方法返回一个long值,该值是指定数组中的最小值。

异常:如果数组为空,则该方法引发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…-



相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Java Guava | Longs.min() method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。