creating and initializing List at the same time
Sometimes we want to create and initialize a List like ArrayList or LinkedList in one
line much like creating an array and initializing it on the same line. If you look at The array on Java programming language you can create and initialize both primitive
and object arrays 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 a lot of code. This is also one of the
tricky
Java question sometimes appears in Interview as Write Java code to create and initialize ArrayList in the 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 a lot of time while creating a test
program or quickly trying some stuff.
Java Tip to create and initialize List in one line
In our post 3
ways to convert Array to ArrayList in Java, we have discussed Arrays.asList() method.
You can use Arrays.asList() method to create and initialize List at the same
line. java.util.Arrays class act as a 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 the 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 a List in one line in Java
* Arrays.asList() our go-to method to initialize List
* in the 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 a List in one line in Java
* Arrays.asList() our go-to method to initialize List
* in the 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 a List in the 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 that doesn't allow you to
add or remove elements from it. add() and remove() method
will throw UnSupportedOperationException if you try
to add or remove elements from the List.
Some programs mistook this List as a read-only List, which is not because it allows set() operation which can be used to change elements in the List. Only legitimate way to create read-only Collection is Collections.unmodifiableXXX() method.
You can also see these Java Collections and Stream Courses to learn more about ArrayList and other collection classes in Java.
That's all on this Java tip about how to create and initialize a List in the same line in Java. This is a great tip that can save a lot of coding time if
you frequently create and initialize a List with test data. You can use this trick to quickly create ArrayList with test data for your unit testing and demos. It really saves a lot of keystrokes in long run.
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