java 数据结构

java 数据结构_第1张图片
Java collection cheat sheet.PNG

Programming to Interface:

For the declared type of a variable, it is best to use the highest class that you're going to need.

List myList = new ArrayList();
Instead of
ArrayList myList = new ArrayList();

Reasons:
(1) "通用性" The first has the advantage that the implementation of the List
can change (LinkedList for example), without affecting the rest of the code.
(2) "规范性" List is an interface--like a template of sorts--that ArrayList is said to inherit. It is a contract that says "anytime you use a List implementation, you can expect these methods to be available". In the case of List, the methods are things like add, get, etc.

Note: you can only call methods and reference members that are defined in the List interface, for example, you cannot invoke a method ensureCapacity on list because this is an additional functionality supported only by ArrayList.

你可能感兴趣的:(java 数据结构)