Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are arrays asList () in Java?
3.
Syntax of Arrays asList() in Java
4.
Parameters of Arrays asList() in Java
5.
Return Value of Arrays asList() in Java
6.
Examples of Java Arrays asList Method
6.1.
Java
6.2.
Java
6.3.
Java
6.4.
Java
6.5.
Java
7.
Frequently Asked Questions
7.1.
What does array asList () contain?
7.2.
What is the difference between ArrayList and array asList in Java?
7.3.
What is the purpose of asList?
7.4.
What is the use of java arrays asList function if it returns only fixed length list?
7.5.
How can the Java arrays asList method be used to initialize a fixed-size list?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Arrays asList() in Java

Author Nikhil Joshi
0 upvote

Introduction

Hey Ninjas, Welcome back!

Suppose you are dealing with arrays in Java, but some program functionality requires you to convert the array to a list. Do you know the method which acts as a bridge between array-based and collection-based APIs in Java? Not to worry. 

Java Arrays asList

In this article titled “Java Arrays asList”, we will look through the intricacy of the asList function in Java. We are going to see several examples of how to use the method. 

Let’s start this journey!

Also see, Duck Number in Java and Hashcode Method in Java.

What are arrays asList () in Java?

The Java arrays asList method bridges the old array-based API and the new Collections API. The new collection API consists of several functions that can improve performance and reduce the program time. We must convert our array to the ArrayList to use this collection API.

Also see, Swap Function in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax of Arrays asList() in Java

Following is the syntax of Java arrays asList method:

public static <T> List<T> asList(T... a)

 

You can pass an array of any type (T), and the method will return a List<T> containing the elements of that array.

Parameters of Arrays asList() in Java

In Java, the Arrays.asList() method accepts one parameter, which is the array you want to convert into a List.

Return Value of Arrays asList() in Java

The Java Arrays.asList() method returns a List view of the provided array. 

Examples of Java Arrays asList Method

Code 1. asList method does not support primitive data types. We need to use wrapper classes. The following code demonstrates the fact:

  • Java

Java

import java.util.Arrays;
import java.util.List;


public class AsListExamples {
   public static void main(String[] args) {
       // an array of primitive data type
       int[] arr = {1,2,34,56,78,89};
      
       // Trying to create a list using Java arrays asList
       List<Integer> list = Arrays.asList(arr);
       printList(list);
   }
   private static void printList(List<Integer> list){
       for(int i=0; i< list.size(); i++){
           System.out.print(list.get(i) );
       }
       System.out.println();
   }
}


This code will give a compilation error because the java arrays asList method does not support primitive data types.

Output

output image

 

Corrected Code

  • Java

Java

import java.util.Arrays;
import java.util.List;


public class AsListExamples {
   public static void main(String[] args) {
       // an array of primitive data type
       Integer[] arr = {1,2,34,56,78,89};


       // Trying to create a list using java arrays asList
       List<Integer> list = Arrays.asList(arr);
       printList(list);
   }


   private static void printList(List<Integer> list){
       for(int i=0; i< list.size(); i++){
           System.out.print(list.get(i)+" ");
       }
       System.out.println();
   }
}


Output

output image

Instead of using primitive data types like int, char, long etc., use wrapper classes like Integer, Character, Long etc.

 

Code 3. The list returned by the java arrays asList method is of fixed size. It is not possible to add or remove items from that list. The following code demonstrates the fact:

  • Java

Java

import java.util.Arrays;
import java.util.List;


public class AsListExamples {
   public static void main(String[] args) {
       // an array of primitive data type
       Integer[] arr = {1,2,34,56,78,89};


       // Trying to create a list using java arrays asList
       List<Integer> list = Arrays.asList(arr);


       // Trying to perform add and remove function on the list
       list.add(24);
       list.remove(2);


       printList(list);
   }
   private static void printList(List<Integer> list){
       for(int i=0; i< list.size(); i++){
           System.out.print(list.get(i)+" ");
       }
       System.out.println();
   }
}


This code will result in a runtime error because the list returned by the java arrays asList method is of fixed size. It is not possible to add or remove items from the list.

Output

output image

 

Code 4set(index,newValue) function can be used to change the value of a particular element of the returned list. The following code demonstrates the fact: 

  • Java

Java

import java.util.Arrays;
import java.util.List;


public class AsListExamples {
   public static void main(String[] args) {
       // an array of primitive data type
       Integer[] arr = {1,2,34,56,78,89};


       // Trying to create a list using java arrays asList
       List<Integer> list = Arrays.asList(arr);


       System.out.println("List before performing set operation");
       printList(list);
       System.out.println("2nd index element before set operation");
       System.out.println(arr[2]);


       // Trying to perform set function on the list
       list.set(2,101);
      
       System.out.println("List after performing set operation");
       printList(list);
       System.out.println("2nd index element after set operation");
       System.out.println(arr[2]);


       System.out.println("Change in the list is also reflected in the array");
   }
   private static void printList(List<Integer> list){
       for(int i=0; i< list.size(); i++){
           System.out.print(list.get(i)+" ");
       }
       System.out.println();
   }
}


The returned list is of fixed length, but the set function can change its content. We can transform an element at a specified index using the set function.

Output:

output image

But what if our application demands adding or removing items from the returned list? A simple solution is to create a new list using ArrayList(Collection<? extends E> c) constructor.

 

Code 5. If you don’t want a fixed-size list, the simple solution is to create a different copy list of the returned list. The following code demonstrates the fact:

  • Java

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class AsListExamples {
   public static void main(String[] args) {
       // an array of primitive data type
       Integer[] arr = {1,2,34,56,78,89};


       // Trying to create a list using java arrays asList
       List<Integer> list = Arrays.asList(arr);
       List<Integer> newList = new ArrayList<>(list);


       // Trying to perform add and remove function on the new list
       newList.add(24);
       newList.remove(2);


       printList(newList);
   }
   private static void printList(List<Integer> list){
       for(int i=0; i< list.size(); i++){
           System.out.print(list.get(i)+" ");
       }
       System.out.println();
   }
}

 

Output:

output image

Frequently Asked Questions

What does array asList () contain?

In Java, the asList() method is typically used to convert an array into a List. It returns a fixed-size List view of the specified array. 

What is the difference between ArrayList and array asList in Java?

ArrayList is a dynamic, resizable collection in Java, while Arrays.asList() returns a fixed-size List view of an array, making it immutable in size.

What is the purpose of asList?

The purpose of asList() in Java is to convert an array into a fixed-size List, providing a convenient way to use List-based operations on Arrays.

What is the use of java arrays asList function if it returns only fixed length list?

Fixed length can be helpful in many applications. It gives us more robust security since any list changes (addition/removal) are impossible. We may also want to convert the fixed-length array to the list to take advantage of Collection API in Java.

How can the Java arrays asList method be used to initialize a fixed-size list?

We can create a fixed-size list using the asList method by the following syntax.

List<String> batsman = Arrays.asList("Sachin", "Virat", "Rohit");

Conclusion

In conclusion, the Java arrays asList method bridges the old array API and new collection API. It returns a fixed-size list. We can not add or remove elements from the list, but a particular element can be changed using the set function. Apart from that, It only takes an array of the object. So, if we wish to use a primitive data type array, we need to use the wrapper class. The asList function can be used to take advantage of Collection API.

If you want to read more about ArrayList, you can read it here. Read how to make your custom ArrayList

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning!

Live masterclass