Java Reference
In-Depth Information
vidual element values. Lines 29-30 also could have used the enhanced
for
statement
(which we'll demonstrate with collections in other examples).
Line 33 calls method
removeColors
(lines 43-55), passing
list
and
removeList
as
arguments. Method
removeColors
deletes the
String
s in
removeList
from the
String
s
in
list
. Lines 38-39 print
list
's elements after
removeColors
completes its task.
Method
removeColors
declares two
Collection<String>
parameters (lines 43-
44)—any two
Collection
s containing
String
s can be passed as arguments. The method
accesses the elements of the first
Collection
(
collection1
) via an
Iterator
. Line 47 calls
Collection
method
iterator
to get an
Iterator
for the
Collection
. Interfaces
Collec-
tion
and
Iterator
are generic types. The loop-continuation condition (line 50) calls
Iterator
method
hasNext
to determine whether there are more elements to iterate
through. Method
hasNext
returns
true
if another element exists and
false
otherwise.
The
if
condition in line 52 calls
Iterator
method
next
to obtain a reference to the
next element, then uses method
contains
of the second
Collection
(
collection2
) to
determine whether
collection2
contains the element returned by
next
. If so, line 53 calls
Iterator
method
remove
to remove the element from the
Collection
collection1
.
Common Programming Error 16.1
If a collection is modified by one of its methods after an iterator is created for that collec-
tion, the iterator immediately becomes invalid—any operation performed with the itera-
tor fails immediate and throws a
ConcurrentModificationException
. For this reason,
iterators are said to be “fail fast.” Fail-fast iterators help ensure that a modifiable collec-
tion is not manipulated by two or more threads at the same time, which could corrupt the
collection. In Chapter 23, Concurrency, you'll learn about concurrent collections (package
java.util.concurrent
) that can be safely manipulated by multiple concurrent threads.
Software Engineering Observation 16.4
We refer to the
ArrayList
s in this example via
List
variables. This makes our code more
flexible and easier to modify—if we later determine that
LinkedList
s would be more ap-
propriate, only the lines where we created the
ArrayList
objects (lines 14 and 21) need
to be modified. In general, when you create a collection object, refer to that object with a
variable of the corresponding collection interface type.
Type Inference with the
<>
Notation
Lines 14 and 21 specify the type stored in the
ArrayList
(that is,
String
) on the left and
right sides of the initialization statements. Java SE 7 introduced
type inferencing
with the
<>
notation—known as the
diamond notation
—in statements that declare and create ge-
neric type variables and objects. For example, line 14 can be written as:
List<String> list =
new
ArrayList<>();
In this case, Java uses the type in angle brackets on the left of the declaration (that is,
String
) as the type stored in the
ArrayList
created on the right side of the declaration.
We'll use this syntax for the remaining examples in this chapter.
Figure 16.3 demonstrates various operations on
LinkedList
s. The program creates two
LinkedList
s of
String
s. The elements of one
List
are added to the other. Then all the
String
s are converted to uppercase, and a range of elements is deleted.