How to initialize ArrayList with Array in Java | Copy from Array to List - One Liner Example

Initializing list while declaring it is very convenient for quick use. If you have been using Java programming language for quite some time then you must be familiar with syntax of array in Java and how to initialize an array in the same line while declaring it as show below:

String[] oldValues = new String[] {"list" , "set" , "map"};

or even shorter :

String[] values = {"abc","bcd", "def"};

Similarly we can also create List  and initialize it at same line, popularly known as initializing List in one line example. Arrays.asList() is used for that purpose which returns a fixed size List backed by Array. By the way don’t confuse between Immutable or read only List which doesn’t allow any modification operation including set(index) which is permitted in fixed length List.Here is an example of creating and initializing List in one line :

Java program to create and initialize List in one line

How to create and initialize List in one line in Java with ArrayHere is a Java program which creates and initialize List in one line using Array. Arrays.asList() method is used to initialize List in one line and it takes an Array which you can create at the time of calling this method itself. Arrays.asList() also use Generics to provide type-safety and this example can be written using Generics as well.

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

 * How to create and initialize List in same line, Similar to Array in Java.
 * Arrays.asList() method is used to initialize a List from Array but List
 * returned by this method is a fixed size List and you can not change its size.
 * Which means adding and deleting elements from List is not allowed.
 * @author Javin Paul

public class ListExample {

    public static void main(String args[]) {
      //declaring and initializing array in one line
      String[] oldValues = new String[] {"list" , "set" , "map"};
      String[] values = {"abc","bcd", "def"};
      //initializing list with array in java
      List init = Arrays.asList(values);
      System.out.println("size: " + init.size() +" list: " + init);
      //initializing List in one line in Java
      List oneLiner = Arrays.asList("one" , "two", "three");
      System.out.println("size: " + init.size() +" list: " + oneLiner);
      //List returned by Arrays.asList is fixed size and doesn't support add or remove
      oneLiner.add("four"); //will throw java.lang.UnsupportedOperationException
      //oneLiner.remove("one"); //also throw java.lang.UnsupportedOperationException

size: 3 list: [abc, bcd, def]
size: 3 list: [one, two, three]
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:131)
        at java.util.AbstractList.add(AbstractList.java:91)
        at test.ExceptionTest.main(ExceptionTest.java:32)

As shown in above example its important to remember that List returned by Arrays.asList() can not be used as regular List for further adding or removing elements. Its kind of fixed length List which doesn't support addition and removal of elements. Nevertheless its clean solution for creating and initializing List in Java in one line, quite useful for testing purpose.

Related Java Collection tutorials from Javarevisited Blog


Sudhanshu said...

If you are using Google Collections aka Google Guava, you can initialize any kind of ArrayList in one line e.g. Integer, String, Double, Float, Long or any arbitrary type. Google Collection provides a utility class Lists, which has several factory method to return ArrayList of any type e.g.

List>Integer< listOfNumbers = Lists.newArrayList(1,2,3,4,5);

Anonymous said...

The ArrayList returned by Arrays.asList() is not your regular list, its a fixed length arraylist where you cannot add or remove elements, though you can replace elements using set() mehtod. If you want to create a regular ArrayList, just use a copy constructor like below

List mylist = new ArrayList((Arrays.asList(1,2 ,3,4));

this will create a fixed length list of integers and then copy content to a new regualr arraylist

Post a Comment