The List is an order collection in Java that extends the Collections interface. A list is an interface, so you cannot directly instantiate it. However, you can create a List by implementing different built-in classes like “ArrayList”, “Stack”, “LinkedList”, and “Vector”. In this tutorial, you will learn different list-creation methods with appropriate examples.
How to Create a List in Java
You can create a list in Java using one of the following methods:
- Method 1: Creating a List Using the new Operator
- Method 2: Creating a List Using Arrays.asList()
- Method 3: Creating a List Using List.of()
- Method 4: Creating a List Using Stream.of() and Collectors.toList()
- Method 5: Creating a List Using Collections Class
Let’s learn each of the listed methods using suitable examples.
Method 1: Creating a List Using the new Operator
The List is an interface in Java whose instance can be created using the new Operator as follows:
//Creating a List Using ArrayList
List listName = new ArrayList();
//Creating a List Using LinkedList
List listName = new LinkedList();
//Creating a List Using Stack
List listName = new Stack();
//Creating a List Using Vector
List listName = new Vector();
Also, you can create a List of a specific type such as Integer, String, etc. In such a case, the above syntax will be modified as follows:
//Creating a List Using ArrayList
List<dataType> listName = new ArrayList<dataType>();
//Creating a List Using LinkedList
List<dataType> listName = new LinkedList<dataType>();
//Creating a List Using Stack
List<dataType> listName = new Stack<dataType>();
//Creating a List Using Vector
List<dataType> listName = new Vector<dataType>();
Once a List is Created, you can initialize it using the add() method as follows:
listName.add(element);
Let’s put these concepts into practice to get a better sight of creating and initializing a list in Java.
Example 1: Creating a List Without Specifying the Elements Type
In the below code, we create two lists by implementing ArrayList and Stack classes, respectively:
import java.util.*;
public class ExampleClass {
public static void main(String[] args){
//List Creation with ArrayList
List arrList = new ArrayList<>();
arrList.add("Hello");
arrList.add("Hi");
arrList.add("Welcome");
System.out.println("arrList ==> " + arrList.toString());
//List Creation with Stack
List stackList = new Stack<>();
stackList.add(1);
stackList.add(11);
stackList.add(21);
System.out.println("stackList ==> " + stackList.toString());
}
}
Code Explanation
- We create an ArrayList and a Stack using the new operator (without specifying any data type).
- After this, we use the add() method to initialize each list with some values.
- Finally, we use the print() method with the toString() method to print the lists’ elements.
Example 2: Creating a List by Specifying the Elements Type
The above example shows that a list can be created without specifying any data type. However, Java experts recommend type-safe List creation to avoid any unexpected errors or ambiguity.
In the below-given code snippet, we create an ArrayList of type Integer and a LinkedList of type String:
import java.util.*;
public class ExampleClass {
public static void main(String[] args){
//List Creation with ArrayList
List<Integer> arrList = new ArrayList<>();
arrList.add(100);
arrList.add(123);
arrList.add(272);
System.out.println("arrList ==> " + arrList.toString());
//List Creation with Stack
List<String> linkList = new Stack<>();
linkList.add("John");
linkList.add("Joseph");
linkList.add("Johnson");
System.out.println("linkList ==> " + linkList.toString());
}
}
Code Explanation
- This time, we create an ArrayList and a Linked using the new operator (by specifying their respective data type).
- After this, we use the add() method to initialize ArrayList with integers and LinkedList with some string values.
- Finally, we use the print() method with the toString() method to print the lists’ elements.
Method 2: Creating a List Using Arrays.asList()
The asList() method of the Arrays class lets us create and initialize a list in one line. This method creates an unmodifiable(immutable) array.
Example 1: Creating an Immutable List Using Arrays.asList()
Let’s learn how the asList() method helps us create and initialize a List in a single step:
import java.util.*;
public class ExampleClass {
public static void main(String[] args){
//List Creation and Initialization with asList()
List<String> linkList = Arrays.asList("Alex", "Joseph", "Ambrose");
System.out.println("linkList ==> " + linkList.toString());
}
}
Executing the above code will generate the following output:
Example 2: Creating a Mutable List Using Arrays.asList()
You can also create a mutable list using the asList() method. To do that, you must use the asList() method with the collaboration of the new operator:
import java.util.*;
public class ExampleClass {
public static void main(String[] args){
//List Creation with ArrayList
List<String> arrList = new ArrayList<>(
Arrays.asList("Seth", "Joseph", "Paul"));
System.out.println("Original arrList ==> " + arrList.toString());
arrList.add("Alexa");
System.out.println("Modified arrList ==> " + arrList.toString());
}
}
This time we use the new operator alongside the asList() method to create and initialize a list. In the next line, we use the println() method to print each list element on the console. To ensure the list’s mutability, we invoke the add() method on the list to add a new element to it:
A new element has been successfully added to the list, which proves the list’s mutability.
Method 3: Creating a List Using List.of()
Java 9 introduces a new method named “List.of()” that accepts any number of arguments and creates an immutable list from them.
Example 1: Creating an Immutable/Unchangeable List Using List.of()
In the following code, we create a String-type immutable list using the List.of() method:
import java.util.*;
public class ExampleClass {
public static void main(String[] args){
//List Creation with of() List<String> newlist = List.of("John", "Alex", "Seth", "Joseph", "Paul");
System.out.println("newlist ==> " + newlist.toString());
}
}
The output snippet verifies the list creation using the List.of() method:
Example 2: Creating a Mutable List Using List.of()
You can use the new operator along with the “List.of()” method to create a modifiable list:
import java.util.*;
public class ExampleClass {
public static void main(String[] args){
//List Creation with ArrayList and Initialization with of()
List<String> newList = new ArrayList<>
(List.of("John", "Alex", "Seth", "Joseph", "Paul"));
System.out.println("Original list ==> " + newList.toString());
newList.add("Dean");
System.out.println("Updated list ==> " + newList.toString());
}
}
In the given code, we first create a list with the collaboration of the new operator and the “of()” method. Then we use the add() method to add a new element to it.
The output verifies that the created list is mutable in nature.
Method 4: Creating a List Using Stream.of() and Collectors.toList()
You can use the “stream.of()” method to generate a stream of objects and then use the “Collectors.toList()” method to collect the steam objects as a list.
Example 1: Creating a Mutable List Using Stream.of() and Collectors.toList()
In the following example, first, we import essential classes from the util package:
import java.util.*;
import java.util.stream.*;
public class ExampleClass {
public static void main(String[] args){
//List Creation with Stream.of()
List<String> newList = Stream.of("John", "Alexa", "James", "Joseph", "Paul").collect(Collectors.toList());
System.out.println("List ==> " + newList);
}
}
In the main() method, we create a stream of objects using the “Stream.of()” method. After this, we use the Collectors.toList() to collect it as a list. Finally, we print the collected list elements on the console:
Example 2: Creating an Immutable List Using Stream.of() and Collectors.toList()
You can use the “Stream.of()” and “Collectors.toList()” methods to create an immutable list. For this purpose, you must use the collectingAndThen() method and pass it the reference of the unmodifiableList() method. Doing this will create an unmodifiable list:
import java.util.*;
import java.util.stream.*;
public class ExampleClass {
public static void main(String[] args) {
//List Creation with Stream.oF() and toList()
List<Integer> newList = Stream.of(172, 236, 124, 411, 500).collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
System.out.println("newList: " + newList.toString());
}
}
The output shows the list’s elements on the console:
Method 5: Creating a List Using Collections Class
In Java, the built-in Collections class provides several static methods that operate on or retrieve collections. Also, it offers different methods that let us create a list. Some well-known list-creation methods of this class are illustrated in the following table:
Method | Description |
emptyList() | This method creates an unmodifiable(immutable) empty list. |
addAll() | This method lets us add the specified elements to a collection(list). We can invoke this method to initialize a list. |
singletonList() | It lets us create a list with only one element. |
unmodifiableList | It allows us to create an unmodifiable/immutable list. |
Let’s see the practical use of the above-stated methods via the following example.
Example 1: Creating a List Using Collections.emptyList()
In the following code, we use the empty() method of the Collections class to create an empty list:
import java.util.*;
public class ExampleClass {
public static void main(String[] args) {
//List Creation with emptyList()
List<String> newList = Collections.emptyList();
System.out.println("newList ==> " + newList);
}
}
Note: You must use the emptyList() method only when you have to create an immutable empty list. Because this empty list can’t be initialized/modified.
Example 2: Creating a List Using Collections.addAll()
In the below code example, first, we create a list using the new operator and then initialize it with string values:
import java.util.*;
public class ExampleClass {
public static void main(String[] args) {
//List Creation with ArrayList and Initialization with addAll()
List<String> newList = new ArrayList<String>();
Collections.addAll(newList, "Anees", "Asghar", "javabeat");
System.out.println("newList ==> " + newList);
}
}
Finally, we print the elements of the created ArrayList on the console using the println() method:
Example 3: Creating a List Using Collections.singletonList()
If you want to create a list with only one element, use the singletonList() method, as follows:
import java.util.*;
public class ExampleClass {
public static void main(String[] args) {
//List Creation and Initialization with singletonList()
List<String> newList = Collections.singletonList("Anees Asghar");
System.out.println("newList ==> " + newList);
}
}
Note: Trying to add more than one element to a singleton list will result in an error.
Example 4: Creating a List Using Collections.unmodifiableList()
In the following code, we use the unmodifiableList() method of the Collections class to create an immutable list. Within this method, we use the asList() method to initialize the list:
import java.util.*;
public class ExampleClass {
public static void main(String[] args) {
//List Creation with unmodifiableList()
List<String> newList = Collections.unmodifiableList(
Arrays.asList("Anees Asghar", "javabeat.net"));
System.out.println("newList ==> " + newList);
}
}
Note: If you try to modify the above-created list, you will encounter an UnsupportedOperationException.
How to Create a List of Lists(Nested List) in Java
Java supports different methods to create a list of lists, such as the “asList()”, “add()” and “stream.of()” methods. For instance, the given code uses the add() method to create a list of lists in Java:
import java.util.*;
public class ExampleClass {
public static void main(String[] args) {
//List Creation with asList()
List<String> firstList = Arrays.asList("Anees", "Asghar");
List<String> secondList = Arrays.asList("javabeat", ".net", "javabeat.net");
List<Integer> thirdList = Arrays.asList(141, 172, 572);
List<List<? extends Object>> nestedList = Arrays.asList(firstList, secondList, thirdList);
System.out.println("nestedList ==> " + nestedList);
}
}
In this code example, we create a couple of string-type lists and an integer-type list using the asList() method. After this, we combine all three lists to create a nested list using the asList() method. In the end, we print the created nested list using the println() method:
That’s all about creating a list in Java.
Conclusion
A list is an ordered collection in Java that can be created using different approaches, such as the new Operator, the “Arrays.asList()” method, or the “List.of()” method. Also, you can use the Java 8 stream to create a Java list. Other than these approaches, the built-in static methods of the Collections class can also be used for list creation. You can create an immutable empty, singleton, or unmodifiable list using the Collections class. In this tutorial, we have discussed various methods along with practical demonstrations to create a list in Java.