| by Arround The Web | No comments

Java Array to List

Arrays and lists are the two most basic data structures in Java. The array provides the common names of the collection which has the same data type. It includes both primitive data types and objects of a class that is based on the array’s definition. On the other hand, the Java lists are the collection frameworks that organize the elements and store them in an object in a manner that maintains the insertion order. There come various scenarios where the Java arrays are required to be converted into Java lists. We can accomplish the conversion of the arrays to lists by using the methods provided by Java.

Example 1:

It is a conventional technique to turn a Java array into a list. The method creates an empty List first, then adds each element in the array to the List. The array size in Java is fixed. So, it returns the fixed size list too after conversion.

Initially, we imported the Java packages for use in its methods, interfaces, and classes. Then, we declared a Java class “ArrayToList1”. We have defined a class “ArrayToList1” class with the main() method. Here, we have created a string array “MyArray” where we have inserted the string elements. Before converting the array to a list, we printed the array “MyArray” using the toString() method where the array is passed. Next, we have called the generic interface of the list with the variable “MyList” and the list is currently empty.

We have used the for loop which iterates over each string element in the array “MyArray” and stores all the elements in the new variable “str”. We have added the elements of the array into the list “MyList” by utilizing the add() method and passed the variable “str” which holds all the array elements. Lastly, we have printed the list which is the conversion from the array.

The following is the output which displays the array and the converted array to a list.

Example 2:

The next method to convert the array to a list is by using the Java 8 stream API. This method processes the collection of the object that is sequenced in a way to generate the required outcome. The elements of the array are first transformed into the stream and then by using the stream, we convert the collection of elements to a list.

We have established the class “ArrayToList2” in the above program. The class “ArrayToList2” represents the generic function “convertToList” where the array is converted to a list. We have defined the object “ArrayIs” inside the function’s constructor to specify the element of the list. Firstly, we have used the Arrays.stream() method to get the stream of an array. Then, we have employed the Collectors.toList() method. This method gives a collection of the newly created list from the array stream.

The newly formed list is then collected by the collect() method. Next, we have the main() method where we have inserted the elements into the array. We have printed the initialized array. After that, we called the “convertToList” function and input the array as a parameter for the conversion into the list. The newly created list without changing the elements is printed on the screen.

The newly created list is formed which is the program’s output. The elements cannot be modified while converting the array to a list.

Example 3:

We have seen how an array is converted to a Java list. The given list can also be easily transformed into an array by employing the toArray() method. The list element can only be added to the array when the size of the list is greater or equal to the array size. Otherwise, we have to create a new array to fill the list element. We have performed the same operations below for converting the list to an array.

We have the Java main() method definition inside the class “ArrayToList3”. We have specified the list here and added the four elements which are to be converted. Then, we created the array “MyArray1” of size “4”. The array “MyArry” is passed as a parameter inside the toArray() method for the conversion which is printed by iterating the elements of the array from the for loop. After that, we declared another array “MyArray2” of smaller size and the toArray() method takes that array as an argument. Next, we have printed the newly allocated array of size “4” the same as the list size.

All the arrays are printed with different results on the screen. Note that the second array provides the null value due to its smaller size.

Example 4:

The next approach for converting the array to a list is the addAll() method. The addAll() method includes each element in the specified collection. We can insert elements both individually and as part of an array.

We have created an empty list “ListIs” in the generic function “ArrayToListConversion” which is called in the class “ArrayToList4”. Then, we added the empty list in an array by employing the collections.addAll() method which returns the list after conversion. Next, we have the main() method and we have created an array “arr[]” which is initialized with the three-string elements. The toString() method is used to print the specified array. The “ArrayToConversion” is invoked which takes the array as an argument and converts the array to the list.

The list is populated by the addAll() method in the output below. All of the array’s elements are included in the list.

Example 5:

The asList () method is the next way for changing an array into a list. The asList() method returns the obtained list in this case when the initialized array is passed as an argument.

We have defined an integer array “Digits” inside the main() method of the specified class “ArrayToList5”. Then, we printed the element of the array by iterating it through the for loop. After this, we set the list interface and called the asList() method in the list variable “list”. By using the asList() method to turn the defined array into a list, we passed it as an argument.

We have obtained the desired list from the asList() method as follows.


The conversion of the Java array into the list has been accomplished in this particular article. We have employed the methods which java supports for converting the arrays into the list. From each method, we have obtained the java list. The methods are different from others but return the required list. Furthermore, we have deployed a method where we have converted the list into an array.

Share Button

Source: linuxhint.com

Leave a Reply