Warum sollte letzteres mit List anstelle von ArrayList verwendet werden?
Es ist eine gute Praxis: Programm statt Implementierung Schnittstelle
Durch ArrayList
mit List
ersetzen, können Sie List
Implementierung in Zukunft ändern, da auf Ihrem Geschäftsanwendungsfall unten abhängig.
List<Object> list = new LinkedList<Object>();
/* Doubly-linked list implementation of the List and Deque interfaces.
Implements all optional list operations, and permits all elements (including null).*/
ODER
List<Object> list = new CopyOnWriteArrayList<Object>();
/* A thread-safe variant of ArrayList in which all mutative operations
(add, set, and so on) are implemented by making a fresh copy of the underlying array.*/
ODER
List<Object> list = new Stack<Object>();
/* The Stack class represents a last-in-first-out (LIFO) stack of objects.*/
ODER
einige andere List
spezifische Implementierung.
List
Schnittstelle definiert Vertrag und spezifische Implementierung von List
kann geändert werden. Auf diese Weise sind Schnittstelle und Implementierung lose gekoppelt.
Verwandte SE Frage:
What does it mean to "program to an interface"?
Siehe auch http: // Stackoverflow.com/questions/383947/what-does-it-mean-zu-Programm-zu-einem-Interface – Raedwald