In this article, we will discuss how to convert Arrays into List using Arrays class’s utility asList() method
1. Conversion of Arrays to List using Arrays.asList(); method
Steps :
- Initialize Arrays with sample values, in our example we have got 2 Arrays viz.; Integer Arrays and String Arrays
- First, convert Integer Arrays to ArrayList using static utility method asList(); of Arrays class
- Print converted ArrayList in console using enhanced for-each loop
- Similarly, convert String Arrays to ArrayList and print to console
Method signature:
public static List asList(Object[] oArray);
ConvertArraysIntoList.java
package in.bench.resources.collection;
import java.util.Arrays;
import java.util.List;
public class ConvertArraysIntoList {
public static void main(String[] args) {
Integer[] intArrays = {31, 83, 53, 97, 29, 7, 13, 47, 79};
String[] strArrays = {
"James",
"Bond",
"Michael",
"Pups",
"Jackson",
"Bird"
};
// 1. conversion of Integer Arrays to List
List<Integer> intList = Arrays.asList(intArrays);
System.out.println("1. Conversion of "
+ "Integer Arrays to List<Integer>\n");
// iterating using enhanced for-loop
for(int iVal : intList){
System.out.println(iVal);
}
// 2. conversion of String Arrays to List
List<String> strList = Arrays.asList(strArrays);
System.out.println("\n\n2. Conversion of "
+ "String Arrays to List<String>\n");
// iterating using enhanced for-loop
for(String strVal : strList){
System.out.println(strVal);
}
}
}
Output:
1. Conversion of Integer Arrays to List<Integer>
31
83
53
97
29
7
13
47
79
2. Conversion of String Arrays to List<String>
James
Bond
Michael
Pups
Jackson
Bird
We can also convert List into Arrays using Collection interface’s toArray() method
2. Conversion of List into Arrays using toArrays(); method
Steps :
- Initialize ArrayList with sample values
- Convert ArrayList to Arrays using toArrays(); method, by passing to ArrayList‘s size
- Print converted String[] Arrays in console using enhanced for-each loop
Method signature:
public Object toArrays();
ConvertListIntoArrays.java
package in.bench.resources.java.collection;
import java.util.ArrayList;
import java.util.Arrays;
public class ConvertListIntoArrays {
public static void main(String[] args) {
// creating ArrayList object of type String
ArrayList<String> al = new ArrayList<String>();
// adding elements to ArrayList object
al.add("Narayan Murthy");
al.add("Dinesh");
al.add("Nandan Nilekeni");
al.add("Ashok Arora");
al.add("Shibulal");
al.add("Kris Gopalakrishnan");
al.add("Raghavan");
// creating String Arrays using ArrayList size
// conversion of List to String Arrays
String[] founders = al.toArray(new String[al.size()]);
System.out.println("Conversion of "
+ "List<String> to String Arrays\n");
// print Arrays values
System.out.println(Arrays.toString(founders));
}
}
Output:
Conversion of List<String> to String Arrays
[Narayan Murthy, Dinesh, Nandan Nilekeni, Ashok Arora,
Shibulal, Kris Gopalakrishnan, Raghavan]
Few important points about conversion of Arrays to List:
- This conversion, only provides the List view of Arrays elements; Not altogether separate list
- Performing any operation on elements will reflect on both Arrays as well as List
- But modification operations like adding/removing will throw UnsupportedOperationException
- If we take generic List<Object> or simply List while conversion, then after conversion adding any other type, other than Arrays type will throw ArrayStoreException
Related Articles:
- Java – Conversion of List to Map
- Java – Conversion of Map to List
- Java – Conversion of Arrays to List
- Java – Conversion of List to Arrays
- Java 8 – Conversion of List to Map
- Java 8 – Conversion of Map to List
- Java 8 – Conversion of Arrays to List
- Java 8 – Conversion of List to Arrays
- Java 8 – How to convert HashMap to ArrayList ?
- Java 8 – Conversion of Arrays to Stream
- Java 8 – Conversion of Stream to Arrays
- Java 8 – Convert List to Stream
- Java 8 – Convert Stream to List
- Java 8 – Convert Stream to ArrayList
- Java 8 – Convert Stream to LinkedList
- Java 8 – Convert Stream to HashSet
- Java 8 – Convert Stream to LinkedHashSet
- Java 8 – Convert Stream to TreeSet
- Java 8 – Convert Stream to HashMap
- Java 8 – Convert Stream to LinkedHashMap
- Java 8 – Convert Stream to TreeMap
- Java 8 – Convert Stream to ConcurrentHashMap
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
Happy Coding !!
Happy Learning !!