Reading other people\'s code, I\'ve seen a lot of:
List ints = new ArrayList();
Map map = new HashMap();
For
List ints = new ArrayList();
Map map = new HashMap();
List
and Map
are the interfaces, so any class implementing those interfaces can be assigned to these references.
ArrayList
is one of the several classes (another is LinkedList
) which implement List
interface.
Same with Map
. HashMap
, LinkedHashMap
, TreeMap
all implement Map.
It is a general principle To program for interfaces and not for implementations. Due to this, the programming task becomes easier. You can dynamically change the behavior of the references.
If you write
ArrayList ints = new ArrayList();
HashMap map = new HashMap();
ints
and map
will be ArrayList
and HashMap
only, forever.