Wednesday, December 5, 2012

How to create and initialize List or ArrayList in one line in Java

creating and initializing List in same time
Sometime we want to create and initialize List like ArrayList or LinkedList in one line much like creating array and initializing it on same line. If you look Array on Java programming language you can create and initialize both primitive and object array e.g. String array very easily in just one line but in order to create a List equivalent of that array, you need to type lot of code. This is also one of the tricky Java question some time appears in Interview as Write Java code to create and initialize ArrayList in same line. In this Java tips we will see this trick which allow you to create and initialize List much like an Array. This tip can also save lot of time while creating test program or quickly trying some stuff.

Java Tip to create and initialize List in one line

How to create and initialize List in one line in Java with ExampleIn our post 3 ways to convert Array to ArrayList in Java, we have discussed about Arrays.asList() method. You can use Arrays.asList() method to create and initialize List at same line. java.util.Arrays class act as bridge between Array and List in Java and by using this method you can quickly create a List from Array, which looks like creating and initializing List in one line, as shown in below program.

package test;

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

/**
 *
 * Java program to show How to create and initialize List in one line in Java
 * Arrays.asList() our go to method to initialize List in same line.
 *
 * @author http://java67.blogspot.com
 */

public class ArrayListDemo{

    public static void main(String args[]) {
     
        //You can create and initialize Array in just one line in Java
        String[] coolStringArray = new String[]{"Java" , "Scala" , "Groovy"};
        System.out.println(" Array : " + Arrays.toString(coolStringArray));
   
        //Now If you want to create an ArrayList with three elements
        List<String> notSoCoolStringList = new ArrayList<String>();
        notSoCoolStringList.add("Java");
        notSoCoolStringList.add("Scala");
        notSoCoolStringList.add("Groovy");
     
        //It took four line to create and initialize List
        System.err.println(" List : " + notSoCoolStringList);
     
        //Now here is simple Java tips to create and initialize List in one line
        List<String> coolStringList = Arrays.asList("Java", "Scala", "Groovy");
        System.err.println(" List created and initialized at same line : " + coolStringList);
    }
}

Output:
 Array : [Java, Scala, Groovy]
 List : [Java, Scala, Groovy]
 List created and initialized at same line : [Java, Scala, Groovy]


So this was our Java tip to create and initialize List in same line. Just remember that Arrays.asList() return java.util.List and not ArrayList or LinkedList. Another worth noting point is that List returned by Arrays.asList() is a fixed length list which doesn't allow you to add or remove element from it. add() and remove() method will throw UnSupportedOperationException if you try to add or remove element from List. Some program mistook this List as read only List, which it is not because it allows set() operation which can be used to change element in List. Only legitimate way to create  read only Collection is Collections.unmodifiableXXX() method.

That's all on this Java tip about how to create and initialize List in same line in Java. This is a great tip which can save lot of coding time if you frequently create and initialized List with test data.

 Other Java Collection tutorials from java67 blog

No comments:

Post a Comment

Java67 Headline Animator