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

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

Guava – Longs.asList()方法及实例

Guava的Longs类的Longs.asList()方法接受一个长数组作为参数并返回一个具有固定大小的列表。返回的列表是由作为参数的长数组支持的。这意味着在作为参数传递的数组中的变化将反映在该方法返回的列表中,反之亦然。

语法:

public static List< Long >asList(long…backingArray)

参数: 该方法接受一个强制参数backingArray,它是一个长数组,用于支持列表。

返回值: Longs.asList()方法返回一个固定大小的列表,该列表由作为该方法参数的数组支持。换句话说,该方法返回数组的列表视图。

下面的例子说明了上述方法的实现。

例1:

// Java code to show implementation of// Guava's Longs.asList() method  import com.google.common.primitives.Longs;import java.util.List;  class GFG {      // Driver's code    public static void main(String[] args)    {          // Creating a Long array        long arr[] = { 1, 2, 3, 4, 5 };          // Using Longs.asList() method to wrap        // the specified primitive Long array        // as a List of the Long type        List<Long> myList = Longs.asList(arr);          // Displaying the elements in List        System.out.println(myList);    }}

输出:

[1, 2, 3, 4, 5]

例2:

// Java code to show implementation of// Guava's Longs.asList() method  import com.google.common.primitives.Longs;import java.util.List;  class GFG {      // Driver's code    public static void main(String[] args)    {          // Creating a Long array        long arr[] = { 3, 5, 7 };          // Using Longs.asList() method to wrap        // the specified primitive Long array        // as a List of the Long type        List<Long> myList = Longs.asList(arr);          // Displaying the elements in List        System.out.println(myList);    }}

输出:

[3, 5, 7]

参考资料:

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

相关推荐