The List<String> ls = new ArrayList<>(); assignment reveals a
couple of items to note:
• I've declared variable ls to be of List<String> interface type, and have
assigned to this variable a reference to an instance of the ArrayList class
it is common practice to declare variables to be of interface type. Doing so
eliminates extensive code changes when you need to work with a different
implementation class; for example, List<String> ls = new
LinkedList<>(); . Check out Chapter 2 's “Why Use Interfaces?” section
for more information about this practice.
• The diamond operator <> (whichisnewinJava7)reducesverbositybyforcing
the compiler to infer actual type arguments for the constructors of generic
classes. Without this operator, I would need to specify String as the actu-
al type argument passed to ArrayList<E> , resulting in the more verbose
List<String> ls = new ArrayList<String>(); instead of the
shorter List<String> ls = new ArrayList<>(); .(Idon'tregardthe
The dump() method's enhanced for statement uses iterator() , hasNext() ,
and next() behind the scenes.
When you run this application, it generates the following output:
ls: Sun Mon Tue Wed Thu Fri Sat
ls: Sun Mon Tue Wednesday Thu Fri Sat
ls: Sun Mon Tue Wednesday Thu Sat
The LinkedList class provides a list implementation that is based onlinked nodes.
will introduce you to nodes later in this chapter.)
LinkedList supplies two constructors: