To create an empty list in Java, you can use the Collections.emptyList
method for an immutable list or the new ArrayList()
constructor for a mutable list.
In this example,
Collections.emptyList
method to create an empty immutable list of type String
.emptyList
method returns an empty list.import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> emptyList = Collections.emptyList();
System.out.println("Empty list: " + emptyList);
}
}
Empty list: []
In this example,
new ArrayList()
constructor to create an empty mutable list of type Integer
.new ArrayList()
constructor returns an empty mutable list.import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> emptyMutableList = new ArrayList<>();
System.out.println("Empty mutable list: " + emptyMutableList);
}
}
Empty mutable list: []
In this tutorial, we learned How to create an Empty List in Java language with well detailed examples.