Convert list to array in Java
In this post, you will learn how to convert a list to an array using the Java programming language.
Arrays and lists are the most commonly used data structures in Java. A list is a sequence of indexed and ordered values. Java List is an interface that extends the Collection interface. It contains a list of any type of data object with a comma separated and enclosed within a square bracket. Java List provides control over the position where you can insert an element. In the development process, we may come across a situation where we need to convert a list to an array.
An array is a container that can hold a fixed number of items, and these items should be of the same type. Once an array is created, we cannot change its size. In Java, an array can hold primitives or objects. The elements of an array are stored in a contiguous memory location.
In the Java programming language, there are different ways to convert a list to an array. Here, we have mentioned most of them-
Convert list to array in Java using get() method
The get() method of the list interface returns the element at the specified position in this list. We can use this method to convert list to an array. In this way, we access all the list elements one by one and add them into an array. This is one of the simplest ways to convert a list into an array.
Syntax:public E get(int index)
Here, the parameter 'index' represents the index of the elements to return. The given program demonstrates the use of the get() method to convert a list to an array in a loop.
// Java program to
// Convert a List to an Array
// Using get() method
import java.io.*;
import java.util.LinkedList;
import java.util.List;
class ListToArray {
public static void main(String[] args)
{
// declaring object of List
List list = new LinkedList();
// Adding element to LinkedList
// using add() method
list.add("Sunday");
list.add("Monday");
list.add("Tuesday");
list.add("Wednesday");
// Storing list inside array of strings
String[] myArray = new String[list.size()];
// Converting ArrayList to Array
// using get() method
for (int i = 0; i < list.size(); i++)
myArray[i] = list.get(i);
// Printing elements of array
for (String str : myArray)
System.out.print(str + " ");
}
}
Output of the above code:
Sunday Monday Tuesday Wednesday
Convert list to array using toArray() method
The toArray() method of the ArrayList is used to return an array containing all the elements in the ArrayList in the correct order.
// Java program to
// Convert a List to an Array
// Using get() method
import java.util.*;
public class ListToArray {
public static void main(String[] args)
{
// declaring object of List
List list = new LinkedList();
// Adding element to LinkedList
// using add() method
list.add("Jan");
list.add("Feb");
list.add("Mar");
list.add("Apr");
list.add("May");
// Converting List to array
// using toArray() method
String[] arr = list.toArray(new String[0]);
// Printing elements of array
for (String x : arr)
System.out.print(x + " ");
}
}
Output of the above code:
Jan Feb Mar Apr May
Convert list to array using Stream introduced in Java 8
Here, we have used the stream method of the java.util.Arrays class to get a stream of array elements. Java 8 introduces the Stream API. The Stream API provides many methods which can be pipelined to produce the desired result. We can use the Collectors.toArray() method of the Steam API to convert a list to an array. This method returns a Collector that collects the input elements into a newly created array in an encounter method.
// Importing utility classes
import java.util.*;
public class ListToArray {
public static void main(String[] args)
{
// declaring object of List
List list = new LinkedList();
// Adding element to LinkedList
// using add() method
list.add("Jan");
list.add("Feb");
list.add("Mar");
list.add("Apr");
list.add("May");
// Converting List to array using streams
String[] my_array
= list.stream().toArray(String[] ::new);
// Printing elements of array
for (String x : my_array)
System.out.print(x + " ");
}
}
Output of the above code:
Jan Feb Mar Apr May
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java