Here we will learn How To Convert an Array to a list in Java? we can convert an array to a list in three ways the first way is simply by native manner where iterating each element of the array and keep on storing it in the list, Second is Array.isList() Inbuild function and third by using Array.collect(Collectors.toList()).
Ways To Convert Array In To List
Here we will discuss three ways to convert an array into a list.
Native Method
We can simply iterate the array and keep on storing each element in the list one by one by initializing the list before. To understand follow the example given below.
public class ArrayToListExample1 { public static void main(String args[]) { String array[] = { "Golf", "Soccer", "Baseball", "Volleyball", "Tennis", "Badminton", "Rugby", "Archery", "Skiing"}; List<T> list = new ArrayList<>(); for (T t : array) { list.add(t); } return list; } }
Array.isList() Method
This an inbuild function that could convert a whole array in one go simply by calling an inbuild function to execution follow the example given below and learn how to do so.
public class ArrayToListExample1 { public static void main(String args[]) { String array[] = { "Golf", "Soccer", "Baseball", "Volleyball", "Tennis", "Badminton", "Rugby", "Archery", "Skiing"}; List<T> list = Arrays.asList(arr);
Here we can directly convert the whole array into the list by just passing the array as an argument and it will convert the array to a list.
Using Collections.addAll() Method
As Array.isList() Methodit is also an inbuild function from the library of Collections and to use it we need to import the library of Collections to implement following the example given below.
import java.util.* public class ArrayToListExample1 { public static void main(String args[]) { String arr[] = { "Golf", "Soccer", "Baseball", "Volleyball", "Tennis", "Badminton", "Rugby", "Archery", "Skiing"}; List<T> list = new ArrayList<>(); Collections.addAll(list, arr);
It is how we can directly convert an array to a list
Learn More about How To Convert an Array to a list in Java? at: Convert Array to list
And also visit Java Tutorials to Get the list of problems you might face during solving problems in java and also visit How do I Reverse a List backward? | Python Tutorial for learning more about python and different other topics of programming.