How do I create an empty collection object?

Bookmark this example!  
Category: java.util, viewed: 1338 time(s).

Sometimes you need to return an empty collection from your Java methods. Java java.util.Collections class have three different static instances for creating empty List, Set and Map. There are also methods when you want to create type-safe empty collections. Bellow it the code example.

 
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.Date;
 
public static void main(String args[])
{
  List list = Collections.EMPTY_LIST;
  Set set = Collections.EMPTY_SET;
  Map map = Collections.EMPTY_MAP;
 
  // For the type-safe example use the following methods.
  List<String> s = Collections.emptyList();
  Set<Long> l = Collections.emptySet();
  Map<Date> d = Collections.emptyMap();
}
 
 
Can't find what you are looking for? Join our FORUMS and ask some questions!

Most Viewed Examples

Latest Code Examples

100 Top & Latest


Locations of visitors to this page
eXTReMe Tracker
visitor stats
Get Chitika Premium