Guava Longs类

Guava Longs类

Longs 是一个用于原始类型long的实用类。它提供了Long或Arrays中没有的与long基元相关的静态实用方法。

声明 :

@GwtCompatible(emulated=true)
public final class Longs
extends Object

下表显示了Guava Longs类的字段摘要 :

Guava Longs类

Guava Longs类提供的一些方法是 。

Guava Longs类

异常 :

  • min : 如果数组是空的,会出现IllegalArgumentException。
  • max : 如果数组是空的,会出现IllegalArgumentException。
  • fromByteArray : 如果字节数少于8个元素,则出现IllegalArgumentException。
  • ensureCapacity : 如果minLength或padding为负数,则出现IllegalArgumentException。
  • toArray : 如果集合或其任何元素为空,则会出现NullPointerException。

下表显示了Guava Longs类提供的其他一些方法:

Guava Longs类

下面给出了一些例子,显示了Guava Longs类方法的实现:

例子1:

// Java code to show implementation
// of Guava Longs.asList() method
  
import com.google.common.primitives.Longs;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        long arr[] = { 3L, 4L, 5L, 6L, 7L };
  
        // Using Longs.asList() method which wraps
        // the primitive long array as List of
        // long Type
        List<Long> myList = Longs.asList(arr);
  
        // Displaying the elements
        System.out.println(myList);
    }
}

输出:

[3, 4, 5, 6, 7]

例子2:

// Java code to show implementation
// of Guava Longs.toArray() method
  
import com.google.common.primitives.Longs;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        List<Long> myList = Arrays.asList(3L, 4L, 5L, 6L, 7L);
  
        // Using Longs.toArray() method which
        // converts a List of Longs to an
        // array of long
        long[] arr = Longs.toArray(myList);
  
        // Displaying the elements
        System.out.println(Arrays.toString(arr));
    }
}

输出:

[3, 4, 5, 6, 7]

例子3:

// Java code to show implementation
// of Guava Longs.concat() method
  
import com.google.common.primitives.Longs;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        long[] arr1 = { 3L, 4L, 5L };
        long[] arr2 = { 6L, 7L };
  
        // Using Longs.concat() method which
        // combines arrays from specified
        // arrays into a single array
        long[] arr = Longs.concat(arr1, arr2);
  
        // Displaying the elements
        System.out.println(Arrays.toString(arr));
    }
}

输出:

[3, 4, 5, 6, 7]

例子4:

// Java code to show implementation
// of Guava Longs.contains() method
  
import com.google.common.primitives.Longs;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        long[] arr = { 3L, 4L, 5L, 6L };
  
        // Using Longs.contains() method which
        // checks if element is present in array
        // or not
        System.out.println(Longs.contains(arr, 4L));
        System.out.println(Longs.contains(arr, 7L));
    }
}

输出:

true
false

例子5:

// Java code to show implementation
// of Guava Longs.min() method
  
import com.google.common.primitives.Longs;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        long[] arr = { 3L, 4L, 5L, 6L };
  
        // Using Longs.min() method
        System.out.println(Longs.min(arr));
    }
}

输出:

3

例子6:

// Java code to show implementation
// of Guava Longs.max() method
  
import com.google.common.primitives.Longs;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        long[] arr = { 3L, 4L, 5L, 6L };
  
        // Using Longs.max() method
        System.out.println(Longs.max(arr));
    }
}

输出:

6

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程