The asList() method of java.util.Arrays class is used to return a fixed-size list backed by the specified array.
This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.
This runs in O(1) time.
public static List asList(T... a)
Parameters: This method takes the array a which is required to be converted into a List. Here … is known as varang which is an array of parameters and works similar to an object array parameter.
NOTE : The type of array must be a Wrapper Class(Integer,FLoat, etc) in case of primitive data types(int,float,etc) , i.e you can’t pass int a but you can pass Integer a. If you pass int a,this function will return a List <int a> and not List <Integer> , as “autoboxing” doesn’t happens in this case and int a is itself identified as an object and a List of int array is returned, instead of list of integers , which will give error in various Collection functions .
Return Value: This method returns a list view of the specified array.
Below are the examples to illustrate the asList() method.
The list is: [A, B, C, D]
The list is: [10, 20, 30, 40]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Check whether array has all identical elements using Arrays.asList() and HashSet in Java
- Java Guava | Floats.asList() method with Examples
- Java Guava | Doubles.asList() method with Examples
- Java Guava | Shorts.asList() method with Examples
- Java Guava | Bytes.asList() method with Examples
- Java Guava | Longs.asList() method with Examples
- Java Guava | Chars.asList() method with Examples
- Java Guava | Booleans.asList() method with Examples
- Ints asList() function | Guava | Java
- Java.util.Arrays.parallelSetAll(), Arrays.setAll() in Java
- Java.util.Arrays.equals() in Java with Examples
- Java 8 | Arrays parallelSort() method with Examples
- Arrays.fill() in Java with Examples
- Arrays copyOf() in Java with examples
- Arrays.toString() in Java with Examples
- Arrays.binarySearch() in Java with examples | Set 1
- Arrays.binarySearch() in Java with examples | Set 2 (Search in subarray)
- util.Arrays vs reflect.Array in Java with Examples
- Arrays.sort() in Java with examples
- Java.util.Arrays.deepEquals() in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.