Read Also: How to Print ArrayList in Java
There are 3 ways by which we can achieve our goal:
1. Using java.util.Collections class fields
2. Using java.util.Collections class methods
3. Using new keyword
Both methods and fields of java.util.Collections class create the immutable empty collection, but the main difference between them is that fields do not provide type safety whereas methods provide type safety.
Let's dive deep into the topic:
Java Initialize Empty Collection
1. Using java.util.Collections class fields
According to Oracle docs, given below are the syntax of fields to create an empty collection:
Collections.EMPTY_LIST
Collections.EMPTY_SET
Collections.EMPTY_MAPGiven below is the code example of empty collection fields:
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.Collections;
public class EmptyCollection {
public static void main(String args[]) {
// Fields do not provide type safety
List list1 = Collections.EMPTY_LIST;
System.out.println("list1 size is: "+ list1.size());
Set set1 = Collections.EMPTY_SET;
System.out.println("set1 size is: "+ set1.size());
Map map1 = Collections.EMPTY_MAP;
System.out.println("map1 size is: "+ map1.size());
}
}
Output:
list1 size is: 0
set1 size is: 0
map1 size is: 0
2. Using java.util.Collections class methods
Given below are the syntax of methods to create an empty collection:
Collections.emptyList()
Collections.emptySet()
Collections.emptyMap()Given below is the code example of empty collection methods:
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.Date;
import java.util.Collections;
public class EmptyCollection2 {
public static void main(String args[]) {
// Below Methods provide type safety
List<String> list2 = Collections.emptyList();
System.out.println("list2 size is: "+ list2.size());
Set<String> set2 = Collections.emptySet();
System.out.println("set2 size is: "+ set2.size());
Map<String, Date> map2 = Collections.emptyMap();
System.out.println("map2 size is: "+ map2.size());
}
}Output:
list2 size is: 0
set2 size is: 0
map2 size is: 0
3. Using new keyword
Given below are the syntax of creating empty collection using new keyword:
new ArrayList<>();
new HashSet<>();
new HashMap<>();
Given below is the code example of empty collection using new keyword:
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.Date;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
public class EmptyCollection3 {
public static void main(String args[]) {
// Creating empty collections using new keyword
List<String> list3 = new ArrayList<>();
System.out.println("list3 size is: "+ list3.size());
Set<String> set3 = new HashSet<>();
System.out.println("set3 size is: "+ set3.size());
Map<String, Date> map3 = new HashMap<>();
System.out.println("map3 size is: "+ map3.size());
}
}Output:
list3 size is: 0
set3 size is: 0
map3 size is: 0
That's all for today, please mention in the comments in case you have any questions related to Java initialize empty collection.