Java Program to Convert an Array into a List
Given an array. Your task is to convert the given array into a list in Java.
Examples:
Input: Array: [Geeks, forGeeks, A computer Portal]
Output: List: [Geeks, forGeeks, A computer Portal]
Input: Array: [1, 2, 3, 4, 5]
Output: List: [1, 2, 3, 4, 5]
Arrays: An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In the case of primitives data types, the actual values are stored in contiguous memory locations. In the case of objects of a class, the actual objects are stored in a heap segment.
List: The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector, and Stack classes.
There are numerous approaches to do the conversion of an array to a list in Java. A few of them are listed below.
- Brute Force or Naive Method
- Using Arrays.asList() Method
- Using Collections.addAll() Method
- Using Java 8 Stream API
- Using Guava Lists.newArrayList()
1. Brute Force or Naive Method
In this method, an empty List is created and all elements present in the Array are added to it one by one.
Algorithm:
- Get the Array to be converted.
- Create an empty List
- Iterate through the items in the Array.
- For each item, add it to the List
- Return the formed List
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> List<T> convertArrayToList(T array[])
{
List<T> list = new ArrayList<>();
for (T t : array) {
list.add(t);
}
return list;
}
public static void main(String args[])
{
String array[]
= { "Geeks" , "forGeeks" , "A Computer Portal" };
System.out.println( "Array: "
+ Arrays.toString(array));
List<String> list = convertArrayToList(array);
System.out.println( "List: " + list);
}
}
|
Output
Array: [Geeks, forGeeks, A Computer Portal]
List: [Geeks, forGeeks, A Computer Portal]
2. Using Arrays.asList() Method
In this method, the Array is passed as the parameter into the List constructor with the help of the Arrays.asList() method.
Algorithm:
- Get the Array to be converted.
- Create the List bypassing the Array as a parameter in the constructor of the List with the help of Arrays.asList() method
- Return the formed List
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> List<T> convertArrayToList(T array[])
{
List<T> list = Arrays.asList(array);
return list;
}
public static void main(String args[])
{
String array[]
= { "Geeks" , "forGeeks" , "A computer Portal" };
System.out.println( "Array: "
+ Arrays.toString(array));
List<String> list = convertArrayToList(array);
System.out.println( "List: " + list);
}
}
|
Output
Array: [Geeks, forGeeks, A computer Portal]
List: [Geeks, forGeeks, A computer Portal]
3. Using Collections.addAll()
Since List is a part of the Collection package in Java. Therefore the Array can be converted into the List with the help of the Collections.addAll() method.
Algorithm:
- Get the Array to be converted.
- Create an empty List.
- Add the array into the List by passing it as the parameter to the Collections.addAll() method.
- Return the formed List
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> List<T> convertArrayToList(T array[])
{
List<T> list = new ArrayList<>();
Collections.addAll(list, array);
return list;
}
public static void main(String args[])
{
String array[]
= { "Geeks" , "forGeeks" , "A computer Portal" };
System.out.println( "Array: "
+ Arrays.toString(array));
List<String> list = convertArrayToList(array);
System.out.println( "List: " + list);
}
}
|
Output
Array: [Geeks, forGeeks, A computer Portal]
List: [Geeks, forGeeks, A computer Portal]
4. Using Java 8 Stream API
An ArrayList constructor can take another collection object to construct a new list containing the elements of the specified array.
Algorithm:
- Get the Array to be converted.
- Convert the array to Stream
- Convert the Stream to List using Collectors.toList()
- Collect the formed list using the collect() method
- Return the formed List
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> List<T> convertArrayToList(T array[])
{
return Arrays.stream(array).collect(
Collectors.toList());
}
public static void main(String args[])
{
String array[]
= { "Geeks" , "forGeeks" , "A computer Portal" };
System.out.println( "Array: "
+ Arrays.toString(array));
List<String> list = convertArrayToList(array);
System.out.println( "List: " + list);
}
}
|
Output
Array: [Geeks, forGeeks, A computer Portal]
List: [Geeks, forGeeks, A computer Portal]
5. Using Guava Lists.newArrayList()
Lists.newArrayList() creates a mutable ArrayList instance containing the elements of the specified array.
Algorithm:
- Get the Array to be converted.
- Create an empty List.
- Add the array into the List by passing it as the parameter to the Lists.newArrayList() method.
- Return the formed List
Java
import static com.google.common.collect.Lists.*;
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> List<T> convertArrayToList(T array[])
{
return Lists.newArrayList(array);
}
public static void main(String args[])
{
String array[]
= { "Geeks" , "forGeeks" , "A computer Portal" };
System.out.println( "Array: "
+ Arrays.toString(array));
List<String> list = convertArrayToList(array);
System.out.println( "List: " + list);
}
}
|
Output
Array: [Geeks, forGeeks, A computer Portal]
List: [Geeks, forGeeks, A computer Portal]
Last Updated :
19 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...