The toArray() method of Longs Class in the Guava library is used to convert the long values, passed as the parameter to this method, into a Long Array. These long values are passed as a Collection to this method. This method returns a Long array.
Syntax:
public static long[] toArray(Collection<? extends Number> collection)
Parameters: This method accepts a mandatory parameter collection which is the collection of long values to be converted in to a Long array.
Return Value: This method returns a long array containing the same values as a collection, in the same order.
Exceptions: This method throws NullPointerException if the passed collection or any of its elements is null.
Below programs illustrate the use of toArray() method:
Example 1 :
import com.google.common.primitives.Longs;
import java.util.Arrays;
import java.util.List;
class GFG {
public static void main(String[] args)
{
List<Long> myList
= Arrays.asList(1L, 2L, 3L, 4L, 5L);
long [] arr = Longs.toArray(myList);
System.out.println(Arrays.toString(arr));
}
}
|
Example 2 :
import com.google.common.primitives.Longs;
import java.util.Arrays;
import java.util.List;
class GFG {
public static void main(String[] args)
{
try {
List<Long> myList
= Arrays.asList(2L, 4L, null );
long [] arr = Longs.toArray(myList);
System.out.println(Arrays
.toString(arr));
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
java.lang.NullPointerException
Reference: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/primitives/Longs.html#toArray-java.util.Collection-
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
31 Jan, 2019
Like Article
Save Article