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

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
}
}
Output:
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)
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
}
}
Output:
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 Learn About Linux Blog
No comments:
Post a Comment