Java Collection provides an architecture to store and manipulate a group of objects. The datatype of data can be changed to a general data type like array into Collection. To convert array-based data into Collection based we can use java. util. Arrays class. This class provides a static method asList(T… a) that converts the array into a Collection.
Steps:
- Define a function to write logic
- Take array input from the user
- Convert array input into Collection with help of asList() function.
Methods used:
1. asList(): This method of java.util.Arrays class is used to return a fixed-size list backed by the specified array and acting as a bridge between array-based and collection-based APIs, in combination with Collection.toArray().
This runs in O(1) time.
Example 1:
Java
// Convert an Array into Collection in Java // import java util library import java.util.*; // class for writing logic of the problem public class ArrayToCollection { public static void main(String args[]) { // array input String playersArray[] = { "Virat" , "Sachin" , "Rohit" , "Bumrah" }; // printing input elements for comparison System.out.println( "Array input: " + Arrays.toString(playersArray)); // converting array into Collection // with asList() function List playersList = Arrays.asList(playersArray); // print converted elements System.out.println( "Converted elements: " + playersList); } } |
Output:
Array input: [Virat, Sachin, Rohit, Bumrah] Converted elements: [Virat, Sachin, Rohit, Bumrah]
Example 2:
Java
// Convert an Array into Collection in Java // import java util library import java.util.*; public class ArrayToCollection { public static void main(String args[]) { String countryArray[] = { "India" , "Pakistan" , "Afganistan" , "Srilanka" }; System.out.println( "Array input: " + Arrays.toString(countryArray)); List countryList = Arrays.asList(countryArray); System.out.println( "Converted elements: " + countryList); } } |
Output:
Array input: [India, Pakistan, Afganistan, Srilanka] Converted elements: [India, Pakistan, Afganistan, Srilanka]
2. Collections.addAll() method :
This method is used in converting the given array to the List object. To use this method, we have to import the package java.util.Collections. You can simply specify this by putting import java.util.* for using the Collections method needed for implementing the program.
Step 1: Declare and initialize the array “countryArray”.
Step 2: Declare the list object “countryList”.
Step 3 : Now, use the method Collections.addAll(Collections,Array). Here, the items in the array will be added to the List.
Example
Java
import java.util.*; class GFG { public static void main(String[] args) { String countryArray[] = { "India" , "Pakistan" , "Afganistan" , "Srilanka" }; List<String> countryList = new ArrayList<>(); Collections.addAll(countryList, countryArray); System.out.println( "Converted ArrayList elements: " + countryList); } } |
Converted ArrayList elements: [India, Pakistan, Afganistan, Srilanka]
3.List.of() method:
To use this method, we have to import the package java.util. It is a static method. List.of() returns an immutable list but to make it mutable list we have to specify this inside the constructor of ArrayList.
Step 1: Declare and initialize the array “countryArray”.
Step 2: Declare the list object “countryList”.
Step 3: Inside the constructor of ArrayList(), specify the List.of(array_name) method. It will simply add the array of elements to the list.
Example
Java
import java.util.*; class GFG { public static void main(String[] args) { String countryArray[] = { "India" , "Pakistan" , "Afganistan" , "Srilanka" }; List<String> countryList = new ArrayList<>(List.of(countryArray)); System.out.println( "Converted ArrayList elements: " + countryList); } } |
Converted ArrayList elements: [India, Pakistan, Afganistan, Srilanka]
4.Arrays.stream() method:
To use this method, import the package static java.util.stream.Collectors.toList. Specify the array element in the Arrays.stream() method and convert it into the list using toList() method.
Step 1: Declare and initialize the array “countryArray”.
Step 2: Declare the list object “countryList”.
Step 3: Specify the array name inside the Arrays.stream() method as Arrays.stream(array_name) and collect(toList()) is used to collect all stream and make it into a List instance.
Example
Java
import static java.util.stream.Collectors.toList; import java.util.*; class GFG { public static void main(String[] args) { String countryArray[] = { "India" , "Pakistan" , "Afganistan" , "Srilanka" }; List<String> countryList = Arrays.stream(countryArray).collect(toList()); System.out.println( "Converted ArrayList elements: " + countryList); } } |
Converted ArrayList elements: [India, Pakistan, Afganistan, Srilanka]