Java Collection provides an architecture to store and manipulate the group of objects. The datatype of data can be changed to 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 Collection.
- Define a function to write logic
- Take array input from the user
- Convert array input into Collection with help of asList() function.
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.
Array input: [Virat, Sachin, Rohit, Bumrah] Converted elements: [Virat, Sachin, Rohit, Bumrah]
Array input: [India, Pakistan, Afganistan, Srilanka] Converted elements: [India, Pakistan, Afganistan, Srilanka]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.