Collections.emptyList(),Collections.EMPTY, Collections.EMPTY_LIST, new ArrayList

Collections.EMPTY_LIST:
public class Collections {
    public static final List EMPTY_LIST = new EmptyList<>();
}
 private static class EmptyList<E>
        extends AbstractList<E>
        implements RandomAccess, Serializable {
        private static final long serialVersionUID = 8842843931221139166L;
......
}
Collections.emptyList():
Class Collections{
    public static final <T> List<T> emptyList() {
        return (List<T>) EMPTY_LIST;
    }
    public static final List EMPTY_LIST = new EmptyList<>();
}   
 private static class EmptyList<E>
        extends AbstractList<E>
        implements RandomAccess, Serializable {
        private static final long serialVersionUID = 8842843931221139166L;
......
}
ArrayList:
Class ArrayList{
        transient Object[] elementData;
        private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
        public ArrayList() {
            this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
        }
}

Summary: Collections.EMPTY and Collections.empty() are both empty lists and share the same one, because EMPTY_LIST is statically modified (public static final List EMPTY_LIST = new EmptyList<>();), the difference is that emptyList() can Specify the return type via generics. And new ArrayList(), the object created each time is different, so it will take up space in comparison.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324146481&siteId=291194637