Empty Collections

In this chapter you will learn:

  1. Get empty collection from Collections

Get empty collection from Collections

  • static List EMPTY_LIST
    The empty list (immutable).
  • static Map EMPTY_MAP
    The empty map (immutable).
  • static Set EMPTY_SET
    The empty set (immutable).
  • static<T> List<T> emptyList()
    Returns the empty list (immutable).
  • static<K,V> Map<K,V> emptyMap()
    Returns the empty map (immutable).
  • static<T> Set<T> emptySet()
    Returns the empty set (immutable).
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
//j  a  va  2  s  . c om
public class Main {
  public static void main(String args[]) {
    List list = Collections.EMPTY_LIST;
    Set set = Collections.EMPTY_SET;
    Map map = Collections.EMPTY_MAP;

    List<String> s = Collections.emptyList();
    Set<Long> l = Collections.emptySet();
    Map<Date, String> d = Collections.emptyMap();
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to use Java Comparator to create customized comparison method
  2. TreeSet and Comparator
  3. Get comparator in reverse order
  4. Use reversed Comparator to sort a list
Home » Java Tutorial » Collections
Iterator
ListIterator
Collection unmodifiable
Collection synchronized
Collection singleton
Collection max/min value
Empty Collections
Comparator
Comparable
Enumeration
EnumSet
EnumMap Class
PriorityQueue