Errata

Java Generics and Collections

Errata for Java Generics and Collections

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
Chapter 4 (Declarations): Static Members
First paragraph.

In

"Because generics are compiled by erasure, at run time the classes List<Integer>, List<String>, and List<List<String>> are all implemented by a single class, namely List."

Here List is referred to as a class but it is an interface.

Anders Granlund  Mar 15, 2023 
Printed Page 7
Second Sentence

States that reference type is any "class, instance, or array type." The Java Specification says
that there are three reference types: "class types, interface types, and array types." Therefore
replace instance with interface.

Anonymous   
Printed Page 7
left column of table of Primitive and Reference types in Java

Primitive type should be boolean, not bool.

Anonymous   
Printed Page 7
3rd paragraph

the line

If an expression e of type in appears where a value of type Integer is expected, boxing converts it to new Integer(e) (however, it may cache frequently occuring values).

should be changed to

If an expression e of type in appears where a value of type Integer is expected, boxing converts it to Integer.valueOf(e) (valueOf method may cache frequently occuring values).

Anonymous  Jul 26, 2009 
Printed Page 7
5th paragraph

The paragraph

In practice, the compiler will arrange for the value of new Integer(1) to be cached, as we explain shortly.

may be removed, or replaced with

In practice, at runtime the valueOf method uses cached values for some of the wrapper classes.

Anonymous  Jul 26, 2009 
Printed Page 7
1st paragraph

"A reference type is any class, instance, or array type."
...should be...
"A reference type is any class, interface, or array type."

Anonymous  Oct 31, 2009 
Printed Page 9
4th paragraph


The foreach loop can be applied to any object that implements the interface Iterable<E>(in package java.lang), which in turn refers to the interface Iterator<E> (in package java.util).

may be replaced with

The foreach loop can be applied to any object that implements the interface Iterable<E>(in package java.lang). The Iterable<E> contains a method iterator() which returns Iterator<E> (in package java.util).

Anonymous  Jul 26, 2009 
Printed Page 12
code snippet, fourth line

the rightmost parenthesis must be removed or else the code won't compile

Anonymous   
Printed Page 103
Chapter 7.3 paragraph 3

Printing History: October 2006 First Edition.

The line says "java.lang.reflect.array.newInstance(int.class,size)", however the class
"java.lang.reflect.array" does not exist in Java 5.0. It should be
java.lang.reflect.Array.newInstance(int.class,size).

Futher, the next paragraph says "... the call returns a value of type int[]". Actually the above
call returns the value of type Object, which can be casted to int[].

Anonymous   
Printed Page 104
Example 1.7, line 8

Line 8 of Example 1.7 should be:
Object newobj = obj.getClass().newInstance();
instead of
Object newobj = obj.getClass().getConstructor().newInstance();

There is no "getConstructor()" method in Class and a call to either getConstructor(Class<?>... parameterTypes) or getConstructors() is unnecessary.

Valentin Baca  Apr 07, 2013 
Printed Page 114
2nd senetence

The sentence
"Array creation, instance tests, and casts nowpose no problems, ..."

"nowpose" probably should be "now pose"

Anonymous  May 16, 2012 
Printed Page 119
3rd paragraph, first line

thiry-three should be thirty-three

Svein Egil Nilsen  Aug 31, 2009 
Printed Page 128
line 9 after 9.3

The line says: 'An exception is checked if it is a subclass of RuntimeException or Error'. Of course this must be: 'An exception is unchecked...'

Jan de Ruiter  May 26, 2010 
Printed Page 128
9.3 Function, 1st paragraph

The line,

The relation between a function and the corresponding method is similar to the relation between Comparator and the compareTo method.

Comparator has a method named compare but not compareTo. The compareTo method belongs to Comparable. The line should be changed accordingly.

Ravindra Ranwala  Dec 02, 2020 
Printed Page 133
10th line from page's top (conting blank lines too)

In class Functions, method List<B> applyAll(List<A> list) signature should be missing a second
parameter: Function<A, B, X extends Throwable> f and the code 2 lines below should
be ...result.add(f.apply(x));

Anonymous   
Printed Page 133
Entire page

Example 9.5 on page 133 does not match the text surrounding it and does not compile.

The text describes a single abstract class. The example is an interface and a separate class
with static methods. Additionally, the applyAll method is written as if it were part of the an
abstract Function class (as described in the text), but it is actually a non-static method of
the separate Functions class. It is referenced in a static context by the Functions class.

Anonymous   
Printed Page 143
21st line

public void update(S o, A a);

surely the 'o' makes more sense as an 's'

mat  Aug 15, 2009 
PDF Page 151
Second paragraph

"For example, in the previous expression the first two terms are comparable for low values of N; in fact, for N < 8, the second term is larger."

If N is equal to 7, the first and second terms are equal to 171.50.
It must be N < 7 or N <= 6.

Emanuel Frua  Mar 04, 2016 
Printed Page 154
Example 11.1

Example 11.1 is supposed to be "just about the simplest possible example of how
Iterable can be directly implemented." But it's clearly simpler to implement
Iterator<Integer> and Iterable<Integer> together. We can then avoid constructing an
instance of an anonymous class:

class Counter implements Iterator<Integer>, Iterable<Integer> {

private int count;
private int i = 0;
public Counter (int count) {
this.count = count;
}
public boolean hasNext() {
return i < count;
}
public Integer next() {
i++;
return i;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Iterator<Integer> iterator() {
return this;
}
}

Anonymous   
Printed Page 154
2nd para

The 2nd paragraph should indicate that the provided scenario is one (of many) possible non-thread-safe scenarios[1].

Also, I believe the described/intended scenario is also has the wrong logic. For example, here's what I think should happen (timeline-wise):

A: push()
B: push()
... (assume empty stack, here) ...
A: index++ [ex: 0] //1
B: index++ [ex: 1] //1 (presumes index value gets flushed)
B: stack[1] => B_elt //2
A: stack[1] => A_elt //2
...

In the above scenario, A's value will overwrite B's vaule in stack[1] contrary to the text. Also, the text says that the index will have incremented twice (which might equal 1 depending on visibility) and points to whatever was there before. In fact, it should point to A's elt value and not any previous/default value (again assuming the intended scenario takes place).

[1] For example, another possible scenario is that Thread 1 & 2 never "see" each others updates to index causing it to be incremented just once or maybe not even at all.

Anonymous  Feb 03, 2010 
Printed Page 160
implementation of pop()

The implementation of the pop() method for ArrayStack on page 160 is incorrect. The code executed if the 'if' condition evaluates to true reads:

return stack[index];
index--;

but the index decrement will never be executed because control returns to the caller when the return statement is evaluated, leading to incorrect behavior for pop() and an inevitable stack overflow after 10 pushes regardless of how many pops() are called.

Since the implementation is marked as 'non-thread-safe', the two statements could be combined as:

return stack[index--];

Of course, one would want more bulletproof (and genericized) code anyway if one were making a Stack.

Pete_Clark  Jul 07, 2013 
Printed Page 163
3rd, 4th and 5th example code fragments

The example uses,

List<Integer> l = Array.asList(0,1,2);

But the asList method is available in the Arrays utility class. So, it should be:

List<Integer> l = Arrays.asList(0,1,2);

Also the same issue occurs on page 164, first example code fragment too.

Ravindra Ranwala  May 01, 2020 
Printed Page 170
last paragraph

"used to by ordered collections"
should read
"used by ordered collections"

Anonymous   
Printed Page 170
top of page

List<Integer> l = Array.asList(0,1,2);

should be

List<Integer> l = Arrays.asList(0,1,2);

Anonymous   
Printed Page 172
2nd paragraph, last line

Missing space.
methodCollections.addAll
should be
method Collections.addAll

Svein Egil Nilsen  Sep 02, 2009 
Printed Page 176
first paragraph of section 12.3

"We will go on to look at these three main kinds"
"these" does not refer to anything mentioned previously.
Better would be
"We will go on to look at the three main kinds"

Anonymous   
Printed Page 181
Figure 13.3

The curved arrow starting at 'a' should point to 'b' instead of to 'j' and the curved arrow from
'j' to 'b' should point in the reverse direction.

Anonymous   
Printed Page 182
second full paragraph

In the sentence "These are snapshot iterators; they reflect the state of the set at the time it
was created," the pronoun "it" should be referring to each iterator, but that's not how the
English reads. The english suggests that iterators reflect the state of the set at the time the
set was created, rather than at the time each iterator was created.

Anonymous   
Printed Page 184
Last paragraph

In the last paragraph, second sentence says,

Computing trees borrowa lot of their terminology from genealogical trees, ...

And this has a typo, hence should be corrected as below.

Computing trees borrow a lot of their terminology from genealogical trees, ...

Ravindra Ranwala  May 05, 2020 
Printed Page 192
code snippets at bottom of page

Some of the source markup is leaking through into the rendered text. The words "verb
$NavigableSet$" and "verb$TreeSet$" should presumably be unadorned (i.e. just "NavigableSet" and
"TreeSet").

Anonymous   
Printed Page 192
Figure 14-1. Queue

There's no method called removed() in Queue interface, rather it should be remove()

Ravindra Ranwala  May 10, 2020 
Printed Page 194
Last paragraph

The first sentence says,

In the next section we shall look at the direct implementations of Queue - PriorityQueue and ConcurrentLinkedList

In fact, that should be ConcurrentLinkedQueue as per figure 14-2.

Ravindra Ranwala  May 10, 2020 
Printed Page 205
1st paragraph

The first sentence of the first paragraph says,

Most queue operations respect delay values and will treat a queue with no unexpired elements as if it were empty.

But it should be something like this:

Most queue operations respect delay values and will treat a queue with no expired elements as if it were empty.

So, it should be expired elements, not unexpired elements, that suits the current context.

Ravindra Ranwala  May 12, 2020 
Printed Page 207
4th paragraph

Both occurances of "null" in this paragraph should say "false" instead.

Anonymous   
Printed Page 207
last paragraph

The text states that "the only way of ensuring that variable writes made by one thread are
visible to reads by another is for both writes and reads to take place within blocks
synchronized on the same locks." I think this is incorrect. Declaring the variable as volatile
should also provide the same assurance.

Anonymous   
Printed Page 211
Last paragraph

In the middle of the paragraph, it says,

Their performance characteristics in Figure 14-1 are the same,

In fact, it should be Table 14-1, not Figure 14-1. So, the correct phrase would be,

Their performance characteristics in Table 14-1 are the same,

Ravindra Ranwala  May 13, 2020 
Printed Page 216
Example 15-1. A list based task scheduler

The declaration of constant FORWARD_PLANNING_DAYS should have been marked static. Here's the correct declaration:

private static final int FORWARD_PLANNING_DAYS = 365;

Ravindra Ranwala  Jun 03, 2021 
Printed Page 218
First example

The signature of the method getSubschedule is not correct. It's return type should be ListIterator<StoppableTaskQueue> instead of listIterator<StoppableTaskQueue>. The correct version is given in the sample code though.

Ravindra Ranwala  May 14, 2020 
Printed Page 219
Last example

The last example contains the code fragment,

List<Character>; charList = new ArrayList<Character>();

which is wrong. The semi-colon after the type declaration is not valid and the code does not compile. It should be corrected as below.

List<Character> charList = new ArrayList<Character>();

Ravindra Ranwala  May 18, 2020 
Printed Page 220
Last paragraph

The first sentence of the last paragraph says,

As we mentioned in the discussion of ArrayBlockingQueue (Section 14.2), ...

In fact, ArrayBlockingQueue is discussed in Section 14.3.

Ravindra Ranwala  May 18, 2020 
Printed Page 223
2nd paragraph

Second sentence reads "It optimizes read access, in line with one of our requirement." The last word should be plural, and it should probably read "It optimizes read access, in line with one of our requirements."

Pete_Clark  Jul 09, 2013 
Printed Page 227
1st paragraph of section 15.3

The sentence

"Even though the choice here is much narrower than with lists or even sets, ..."

probably should be

"Even though the choice here is much narrower than with queues or even sets, ..."

Anonymous   
Printed Page 227
Table 15.1 at the bottom

The table says the remove(O) operation of LinkedList performs in O(1). This should be O(n).

Neverwinterx  Sep 06, 2009 
Printed Page 229
First example

The class BoundedSizeMap is a raw type. Since the book emphasizes on effective use of generics and collections framework, it is better to use generics there. Here's a sample implementation using generics.

public class BoundedSizeMap<K, V> extends LinkedHashMap<K, V> {
private int maxEntries;

public BoundedSizeMap(int maxEntries) {
super(16, 0.75f, true);
this.maxEntries = maxEntries;
}

protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > maxEntries;
}
}

Ravindra Ranwala  May 20, 2020 
Printed Page 232
Last line of the 2nd paragraph

Deletions are trickier than with chaining; if key2 and value2 were removed from the table in Figure 13-2, key3 and value3 would have to be moved along to take their place. The Figure 13-2, is wrong and should be corrected as Figure 16-4

Ravindra Ranwala  Jul 01, 2021 
Printed Page 245
under 16.5.1

The book states iterators (of concurrent Map implementations - not clear, the section is about
ConcurrentSkipListMap, but in previous sections nothing is said about iterator of ConcurrentHashMap) are
fail-fast. According to Javadoc, they are weakly consistent:
java.sun.com/javase/6/docs/api/java/util/concurrent/ConcurrentSkipListSet.html
"Iterators are weakly consistent, returning elements reflecting the state of the set at some point at or
since the creation of the iterator. They do not throw ConcurrentModificationException."
java.sun.com/javase/6/docs/api/java/util/concurrent/ConcurrentHashMap.html
"Iterators and Enumerations return elements reflecting the state of the hash table at some point at or
since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException."

Anonymous   
Printed Page 247ff
Chapter 17

While chapter 17 gives the impression that all the methods of the java.util.Collections class
are described, two methods seem to be missing:

<T> Enumeration<T> enumeration(Collection<T> c)

<T> ArrayList<T> list(Enumeration<T> e)

Anonymous