Per evitare NullPointerException indesiderato.
Nel codice, è possibile restituire un normale ArrayList "vuoto" invece di restituire null. In tal modo, continuerai a creare nuovi oggetti (con una capacità predefinita di 10) su ciascuna esecuzione che non è un approccio efficiente alla memoria. Invece di restituire emptyList, la stessa istanza verrà restituita ad ogni richiamo. In questo modo si salva da indesiderati NullPointerException in un modo più efficiente.Ecco l'insulsa dal Javadoc per emptyList:
/**
* Returns the empty list (immutable). This list is serializable.
*
* <p>This example illustrates the type-safe way to obtain an empty list:
* <pre>
* List<String> s = Collections.emptyList();
* </pre>
* Implementation note: Implementations of this method need not
* create a separate <tt>List</tt> object for each call. Using this
* method is likely to have comparable cost to using the like-named
* field. (Unlike this method, the field does not provide type safety.)
*
* @see #EMPTY_LIST
* @since 1.5
*/
fonte
2012-09-26 08:55:14
Possibilmente risposta [qui] (http://stackoverflow.com/questions/5552258/collections-emptylist-vs-new-instance) –
immutabilità è buono per voi ! –