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
import java.util.*;
public class ArrayToCollection {
public static void main(String args[])
{
String playersArray[]
= { "Virat", "Sachin", "Rohit", "Bumrah" };
System.out.println("Array input: "
+ Arrays.toString(playersArray));
List playersList = Arrays.asList(playersArray);
System.out.println("Converted elements: "
+ playersList);
}
}
|
Output:
Array input: [Virat, Sachin, Rohit, Bumrah]
Converted elements: [Virat, Sachin, Rohit, Bumrah]
Example 2:
Java
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);
}
}
|
Output
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);
}
}
|
Output
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);
}
}
|
Output
Converted ArrayList elements: [India, Pakistan, Afganistan, Srilanka]
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...