Java: The role of Collections.empty* and what to pay attention to

1. Benefits

1. If you want to create a new empty List, and this List will not add elements in the future, then use Collections.emptyList().
When new ArrayList() or new LinkedList() is created, it will have an initial size, and it will occupy a memory. Every time it is used, an empty list collection is new, and the waste will add up, and the waste will be serious.

2. For the convenience of coding.
For example, if the return type of a method is List, when there is no result, it returns null, and when there is a result, it returns a list of list collections. In that case, where this method is called, a null judgment is required. Using methods such as emptyList can be convenient for method callers. The return will not be null, eliminating the need for repeated code.

Two, be careful

This empty collection cannot call .add() or .put() to add elements. Because the exception is reported directly. Because the source code of EmptyList inherits the abstract class AbstractList, it simply implements some methods of the collection framework. The last method body called by the add method here is to throw an exception directly.

throw new UnsupportedOperationException();

Here is a simple look at the source code:

 /** 
    * Collections 类里面的方法如下,一步步往下看就是啦 
    */  
   public static final <T> List<T> emptyList() {
    
      
       return (List<T>) EMPTY_LIST;  
   }  
//。。。。。  
   /** 
    * Collections 类里面的方法如下,一步步往下看就是啦 
    */  
   public static final List EMPTY_LIST = new EmptyList<>();  
//。。。。。  
 /** 
    * Collections里面的一个静态内部类 
    */  
   private static class EmptyList<E> extends AbstractList<E> implements RandomAccess, Serializable {
    
      
       private static final long serialVersionUID = 8842843931221139166L;  
  
       public Iterator<E> iterator() {
    
      
           return emptyIterator();  
       }  
       public ListIterator<E> listIterator() {
    
      
           return emptyListIterator();  
       }  
  
       public int size() {
    
    return 0;}  
       public boolean isEmpty() {
    
    return true;}  
  
       public boolean contains(Object obj) {
    
    return false;}  
       public boolean containsAll(Collection<?> c) {
    
     return c.isEmpty(); }  
  
       public Object[] toArray() {
    
     return new Object[0]; }  
  
       public <T> T[] toArray(T[] a) {
    
      
           if (a.length > 0)  
               a[0] = null;  
           return a;  
       }  
  
       public E get(int index) {
    
      
           throw new IndexOutOfBoundsException("Index: "+index);  
       }  
  
       public boolean equals(Object o) {
    
      
           return (o instanceof List) && ((List<?>)o).isEmpty();  
       }  
  
       public int hashCode() {
    
     return 1; }  
  
       // Preserves singleton property  
       private Object readResolve() {
    
      
           return EMPTY_LIST;  
       }  
   }  

In addition to this emptyList, there are similar ones, emptyMap, emptySet and so on. See the picture below for details, it’s all a routine.

Guess you like

Origin blog.csdn.net/qq_29229567/article/details/123727606