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

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]
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.
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.
Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Java Fundamentals: The Java Language
Complete Java Masterclass
Other Java Collection tutorials
from java67 blog
There is one correction required in your article. Calling remove() method on List returned from Arrays.asList() doesn't throw any UnSupportedOperationException but calling add() method will throw the exception.
ReplyDeleteThat's not true, even calling remove() method on List returned by Arrays.asList will throw UnsupportedException as seen below :
DeleteException in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at java.util.AbstractList$Itr.remove(AbstractList.java:374)
at java.util.AbstractCollection.remove(AbstractCollection.java:291)
Only method which works without exception and modifies the list is set(index, value), which you can use to update the List.
Cool!
ReplyDelete