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. If the error was corrected in a later version or reprint the date of the correction will be displayed in the column titled "Date Corrected".

The following errata were submitted by our customers and approved as valid errors by the author or editor.

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

Version Location Description Submitted By Date submitted Date corrected
Page In many of the code examples.
In many of the code examples.

In general there are many small errors in the a lot of the examples needs to be fixed before they can be compiled.

Note from the Author or Editor:
Yes. These are all (hopefully) fixed in the second edition.

Anders Granlund  Mar 26, 2023 
Page Chapter 3 (Comparison and Bounds): Covariant Overriding
The block of code that outlines the definition of the class Object.

class Object {
...
public Object clone() { ... }
}

should be replaced with

class Object {
...
protected Object clone() { ... }
}

Note from the Author or Editor:
Fixed in 2e.

Anders Granlund  Mar 15, 2023 
Page 167
2nd paragraph

Note that phoneTuesdayTasks has the type List<Task> , while tuesdayPhoneTasks has the
more precise type List<PhoneTask> .

The two occurrences of List should be replaced with Collection.

Note from the Author or Editor:
Fixed in new edition

Ravindra Ranwala  May 05, 2021 
Page 91
Last example

Method singleton should have it's type variable declared before the return type of the method. The correct declaration should be something like this.

public static <E> List<E> singleton(E elt) {
return Arrays.asList(elt);
}

Note from the Author or Editor:
Fixed in 2e.

Ravindra Ranwala  Apr 10, 2020 
Page 87
Example 6.1 DeceptiveLibrary

Method intLists, the return variable name should be intLists, not ints. In fact, there's no variable declared with the name ints inside the body of this method. So, this statement is wrong.

return ints;

It should be,

return intLists;

Note from the Author or Editor:
Fixed in 2e.

Ravindra Ranwala  Apr 07, 2020 
Page 78
Paragraph before the last

Another example of the use of unchecked casts to interface legacy and generic code occurred in Section 5.4.1, where we needed an unchecked cast to the element type (E) to make the type of the value returned by the legacy add method match its generic signature.

This paragraph is flawed, since the add method just returns a boolean, not an element of type E. In fact, it is the pop method that returns an element of type E and eventually requires an unchecked cast to E. So, the add method should be replaced with the pop method.

Note from the Author or Editor:
Fixed in 2e.

Ravindra Ranwala  Apr 06, 2020 
Page 36
section 3.3 - 2nd paragraph

The book says, Following good practice, we have also defined a hashCode method, to ensure that equal objects have the same hash code. But the hashCode method is missing in the given source code. Better include this method in the Frutty example code. Here's a sample.

@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + Integer.hashCode(size);
return result;
}

Note from the Author or Editor:
Fixed in 2e.

Ravindra Ranwala  Mar 26, 2020 
Page 29
Last example

In the last example, the statement calling the generic method missing the target variable name.

List<List<?>> = Lists.<List<?>>factory(); // ok

This should be corrected as List<List<?>> list = Lists.<List<?>>factory(); // ok

Note from the Author or Editor:
Fixed in 2e

Ravindra Ranwala  Mar 25, 2020 
Page 27
4th paragraph

The method reverse does not compile. The type parameter T should appear before the return type void of the generic method reverse. Here's the method in error.

public static void <T> reverse(List<T> list) {
List<T> tmp = new ArrayList<T>(list);
for (int i = 0; i < list.size(); i++) {
list.set(i, tmp.get(list.size()-i-1));
}
}

And here's the corrected code.

public static <T> void reverse(List<T> list) {
List<T> tmp = new ArrayList<T>(list);
for (int i = 0; i < list.size(); i++) {
list.set(i, tmp.get(list.size() - i - 1));
}
}

Notice that <T> type parameter appears before the return type void of the generic method reverse.

Note from the Author or Editor:
Fixed in 2e

Ravindra Ranwala  Mar 25, 2020 
Page 30
Last paragraph

The line
class NestedList implements ArrayList<List<?>>{...} // ok
should read
class NestedList extends ArrayList<List<?>>{...} // ok

Note from the Author or Editor:
Fixed

Maurice Naftalin  Sep 11, 2015 
PDF
Page 56
Last paragraph before final display

"However, say we change the methods so that each appends its result to the end of the
argument list rather than returning a value:" -->
"However, consider a different pair of methods, which check whether every element of a list
of integers is zero, or whether every element of a list of strings is the empty string."

Philip Wadler  Jan 15, 2015 
PDF
Page 56
First bullet point

"Appendable & Closeable" should be in fixed-width font.

Philip Wadler  Jan 15, 2015 
Page 84
excerpt of code

1. The method is defined as
public static <T> t[] Array(toCollection<T> c, T[] a){

that should have been

public static <T> t[] toArray(Collection<T> c, T[] a){

Note from the Author or Editor:
Fixed in 1e.

Anonymous  Dec 04, 2013 
Printed
Page 34
Last code block

Sign of comparison is wrong ("")

Maurice Naftalin
 
Dec 02, 2013 
Page 42
3rd para from bottom

"panic.Actually" should be "panic. Actually"

Note from the Author or Editor:
Fixed in 2e

Anonymous  Sep 23, 2013 
Page 35
4th paragraph

lnteger should be Integer (it starts with a lower-case L)

Note from the Author or Editor:
Fixed in 2e

Anonymous  Sep 10, 2013 
Page 32
2nd to last paragraph

"Comparison differs from equality in that >>is<< does not accept a null argument"

>>is<< should be "it" or "Comparison" or "the former"

Note from the Author or Editor:
Fixed in 2e

Anonymous  Sep 10, 2013 
Page 36

Example 3-1 and example 3-2 have the wrong order in the text!

Note from the Author or Editor:
Fixed in 2e

Randolf Rothfuss  Aug 03, 2012 
Page 33
3rd para from bottom of page

The paragraph starts "However, it is always required..."

Omit the word "is" in the second sentence, "Every value [is] compares as the same as itself:"

Note from the Author or Editor:
Fixed in 2e

Thomas Costick  Mar 08, 2011 
Page 56
Section 4.4 on Erasure, after the "class Overloaded" code snippet

In the discussion of erasure and method overloading, it is stated that the return type of the methods (in this case the method sum()) distinguishes two methods from each other. This is a Java 1.5 compiler bug addressed by Java Sun Bug ID: 6182950. The class Overloaded should generate a compile warning when this bug is fixed, since due to erasure both sum() methods have the same name and same argument list, and therefore are indistinguishable. In general, method return types cannot be used to distinguish between overloaded methods.

Note from the Author or Editor:
Fixed in 2e.

Anonymous  Jun 29, 2010 
Page 41
2nd code snippet

The code snippet that creates an instance of Comparator that provides natural ordering (2nd code snippet in page 41) gives compilation error.

I think the corrected code should be something like below :

public static <T extends Comparable<? super T>> Comparator<T> naturalOrder(){
return new Comparator<T>() {
public int compare(final T o1, final T o2) {
return o1.compareTo(o2);
}
};
}

Note from the Author or Editor:
Fixed in 2e

sateesh  Jan 06, 2010 
Page 37
5th paragraph

Below is the text that appears in page 37 while explaining the need to modify the type signature of the method "compareTo" to use "super" wildcard :

"The second example shows why this wildcard is needed. If we want to compare two oranges, we take T in the preceeding code to be Orange:"

In the above text "second" example would refer to Example 3.2. But I could verify that even without any modification to method signature "compareTo" i.e even with the signature :
<T extends Comparable<T>> T max
the example 3.2 would work fine.

But with the above signature Example 3.1 reports a compilation error.
So I think the above line in Page 37, needs to be corrected to :

"The first example shows why this wildcard is needed. .."

or better as :

"The Example 3.1 shows why this wildcard is needed.

Note from the Author or Editor:
Fixed in 2e

sateesh  Jan 05, 2010 
Page 12
code snippet 1

the use of toString() in the assert is slightly dangerous.
since we are relying the implementation of toString of the ArrayList
which is not a method appearing the List interface, and description
of toString exists for ArrayList. There is a chance of someone
mistyping the spaces and the comma positions in the String used
for assertion.

Note from the Author or Editor:
Changed in 2e to use List equality.

Anonymous  Jul 27, 2009 
Printed
Page 139
last code snippet

"abstract class Trust<T extends Trust>"

should be changed to

"abstract class Trust<T extends Trust<T>>"

Note from the Author or Editor:
Fixed!

Anonymous  Jul 26, 2008  Feb 01, 2009
Printed
Page 19
line 2

"as required by the super" should be "as required by the super wildcard"

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Printed
Page 137
last paragraph

"expects an arguement" should be "expects an argument"

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Printed
Page 132
2nd paragraph from bottom

Throwable is a checked exception, so the sentence

"An exception is checked if it is a subclass of Exception but not a subclass of RuntimeException"

should be changed to

"An exception is checked if it is not a subclass of RuntimeException or Error"

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Printed
Page 120
3rd line from bottom

"by the sublist method" should be "by the subList method".

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Printed
Page 12
2nd code snippet and 3rd paragraph

The line

List<Integer> ints = Lists.<Integer>toList();

should be changed to

List<Integer> ints = Lists.toList();

The following paragraph explains why adding "<Integer>" appears to be necessary:

"[W]ithout the type parameter there is too little information for the type inference algorithm to infer the correct type. It infers that the argument to toList is an empty array of an arbitrary generic type rather than an empty array of integers, and this triggers the unchecked warning described earlier." (The latter is probably referring to the "unchecked varargs" warning.)

That's not true.

It is true that without the type parameter, Sun's javac (1.5 and 1.6) issues a warning: "unchecked generic array creation of type T[] for varargs parameter". But it does not issue an error, which means that it accepts the assignment of the result of Lists.toList() to a variable of type List<Integer> without a cast, which means that it correctly inferred the type Integer for the type parameter T of method Lists.toList().

That an unchecked warning is issued is actually a bug in Sun's javac.

The Eclipse compiler (which generally has fewer generics bugs than javac) compiles the line

List<Integer> ints = Lists.toList();

without any warnings or errors.

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008 
Printed
Page 7
second code snippet

The line

ints.add(new Integer(1));

should be changed to

ints.add(Integer.valueOf(1));

That's what Sun's compiler generates (and probably all others). Integer.valueOf() caches small values. The following sentence, which mentions caching, should be changed to explain the use of valueOf().

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Page 50
4th paragraph

According to the Java Language Specification, 3rd ed, �8.4.2, the signature of a method does not include its return type.

In the first sentence,

"if the signatures match exactly"

should be changed to

"if the signatures and return types match exactly"


In the second sentence,

"if the arguments match exactly"

should be changed to

"if the signatures match exactly"

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Page 48
2nd paragraph from bottom

The paragraph explains the code example 3.6, but has the "first" and "second" method the wrong way around.

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008 
Page 47
code snippets

The 'copy' method does not close the given streams if an exception occurs. In general, streams should be closed in 'finally' blocks. Additionally, they should be closed in the code block where they were opened, because otherwise even a 'finally' block is not really reliable. For example, if the third line in the second code snippet throws an exception, the stream opened in the second line will remain open. In a nutshell: A code block should close the streams it opens, but no others.

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Page 42
last code snippet, 2nd line

The parameter declaration "Comparator<E> comp" should be changed to "Comparator<? super E> comp" to make the method applicable in more cases.

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Page 33
several places

The two occurrences of "this generalizes the property for integers" could be replaced by the more general "this generalizes the property for numbers", which is also used on the page.

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Printed
Page 54
code snippet in middle of page

The code snippet contains the method

public static int getCount() { return count; }

This should be changed to

public static synchronized int getCount() { return count; }

The paragraph following the code snippet states "getCount returns the current count", but without synchronization, getCount may return an old value. See the 8th line from bottom on page 161: "Each thread may use a separate memory cache, which means that writes by one thread may not be seen by another unless either they both take place within blocks synchronized on the same lock, or unless the variable is marked with the 'volatile' keyword."

P.S.: In this case, a better solution than 'synchronized' or 'volatile' would be java.util.concurrent.atomic.AtomicInteger.

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Printed
Page 93
7th line from bottom

In the line

newInstance(a.getClass().getComponentType(), c.size());

the expression "c.size()" should be replaced by "size".

(Probably copy & paste error, see the code snippet on page 87.)

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Printed
Page 81
4th paragraph

As far as I know, C# generics are reified, so the statement "unchecked casts in C# are much more dangerous than unchecked casts in Java" is wrong - there are no unchecked casts in C#.

Note from the Author or Editor:
Fixed.

Anonymous  Jul 26, 2008  Feb 01, 2009
Printed
Page 100
1st paragraph

The statement "it often is more robust to use equality (the equals method) [than the == operator]" is true in general, but wrong if only Class objects are concerned (as seems to be implied in this paragraph), because java.lang.Class does not overide equals(). If cls1 is a java.lang.Class object, cls1 == cls2 is always equivalent to cls1.equals(cls2), even if multiple class loaders are involved.

Note from the Author or Editor:
Changed wording to clarify .

Anonymous  Jul 26, 2008  Feb 01, 2009
Page 19
First line

The following error report (listed on the unconfirmed error reports page) is wrong:

{19} First line;
"...which is a subtype of List<? super Number>..." must be read as "...which is a supertype of
List<? super Number>..."

List<Object> is indeed a subtype of List<? super Number>. The quoted sentence is correct as printed in the book.

Note from the Author or Editor:
This is correct.

Anonymous  Jul 24, 2008 
Printed
Page xiv
Obtaining the Example Programs section

This section is incorrect and needs to be replaced with the proper URL to download the code.

ftp://ftp.oreilly.com/published/oreilly/9780596527754
should be:
http://examples.oreilly.com/9780596527754

Also, the section about ftpmail should be completely removed. We don't have ftpmail service.

Anonymous   
Printed
Page 4
last code snippet

In the line
for (int i = 0; i < ints.size; i++) { s += ints[i]; }
the word size should be length. The original fails compilation.

Anonymous   
Printed
Page 5
middle of the page

"guaranteee" should read "guarantee"

Anonymous   
Printed
Page 16
3rd line

"Collections" should be "Collection"

Anonymous   
Page 16
3rd code snippet

A list which is created by method Arrays.asList(T...) cannot be manipulated via method add() of
interface List. So line 3 in this code snippet would cause a
java.lang.UnsupportedOperationException at runtime.
I know that this code snippet would never be runnable because of the compile error in line 2 but
the rest of the code should be as correct as possible.

Note from the Author or Editor:
Changed to use new ArrayList<>() in 2e

Anonymous   
Page 16
Last sentence

The order of the two words "List<Integer>" and "List<Number>" in the last sentence of page 16
should be swapped, because it should be saying that List<Number> is not a subtype of
List<Integer>.

Note from the Author or Editor:
Fixed in 2e.

Anonymous   
Page 19
line 2

"as required by the super" should be "as required by the super wildcard"

Note from the Author or Editor:
Fixed in 2e

Anonymous   
Page 21
Last code snippet - third line

ints.add(3) is ok at compile-time, but fails to execute at runtime. The reason is that the
ArrayList object returned by the asList() method in the first line comes from an inner class of
the Arrays class, that doesn't implement the add(E e) method, required by the contract of the
List interface. So, the call to Arrays$ArrayList.add(E e) always throws an UnsuportedOperation
exception.

Note from the Author or Editor:
Fixed in 2e

Anonymous   
Page 22
second para, after code sample

The section describes the bounding of get and put for generics - "Similarly, you
cannot get anything out from a type declared with an extends wildcard...". I believe
that this should be changed to a "super wildcard".

Note from the Author or Editor:
Fixed in 2e

Anonymous   
Page 23
The 2 code blocks in the middle of the page.

The code is listed as:

List<Integer> ints = Arrays.asList(1,2,3);
List<Number> nums = ints;
nums.put(2, 3.14);

List<Integer> ints = Arrays.asList(1,2,3);
List<? extends Number> nums = ints;
nums.put(2, 3.14);

Line 2 of the first block is an intended compiler error, but line 3 on both blocks calls a "puts" method
that doesn't appear to be a method of the List interface. I'm assuming a java.util.List is the correct
interface.

Note from the Author or Editor:
Fixed in the 2e.

Anonymous   
Page 24
3rd paragraph

"Arrays of primitive type are much more efficient ... assignments into such an array need not
check for an array store exception, because primitive types don't have subtypes."

As per JLS 3ed 4.10, primitive types ARE arranged in a subtype hierarchy. Array types whose
component types are primitive are not arranged in a subtype hierarchy, so a more accurate clause
would be "because arrays of primitive type don't have subtypes."

Note from the Author or Editor:
Fixed in the 2e

Anonymous   
Page 29
4th paragraph

Change "at" to "as" in "elements from the inner lists at any other type".

Note from the Author or Editor:
Fixed in 2e

Anonymous   
Printed
Page 31
first line

"more-advanced" should be "more advanced"

Anonymous   
Page 31
3.1 2nd paragraph "The compareTo method..."

The book contains :
"The compareTo method returns a value thatis negative, zero, or positive depending upon whether
the **argument** is less than, equal to, or greater then the given object."

Something is confusing in this sentence.

An extract from the Java API at :
java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html

int compareTo(T o)

Compares this object with the specified object for order. Returns a negative integer, zero, or a
positive integer as this object is less than, equal to, or greater than the specified object.

Is the "given object" the argument o of type T (bad terminology) ? Or is it the reference on
which the method is called via the dot operator (contradicts the Java contract) ?

Note from the Author or Editor:
Fixed in 2e

Anonymous   
Page 33
5th last paragraph

The sentence "Every value is compares as the same as itself" does not make sense.

Note from the Author or Editor:
Fixed in 2e

Anonymous   
Page 33
after first example

What property is being generalized here? How can x<y iff y<x

Note from the Author or Editor:
Fixed in 2e

Anonymous   
Printed
Page 35
compile-time error entry

should be 3.14 not 3,14 since that is not the compile error being demonstrated

Anonymous   
Page 37
Comparator

The text says the Comparator interface contains a single method:

public int compare (T o1, t o2);

It contains two methods, the other method being:

public boolean equals(Object obj);

Note from the Author or Editor:
Fixed in 2e

Anonymous   
Page 38
class Fruit

In the examples on pages 38 and 39, the classes define the "hash" method and call the "hash"
method on a String. This won't compile. Presumably what was intended was to define and call the
"hashCode" method.

Note from the Author or Editor:
Fixed in 2e

Anonymous   
Page 41
last paragraph

The text states "By the contract for comparators, it would be equivalent to leave the arguments
in the original order but negate the result."

This is not quite true if we take the meaning of "negate" to be "apply the unary minus
operator". The negation of Integer.MIN_VALUE is not representable in a Java int. The value that
results from applying the unary minus operator to Integer.MIN_VALUE is Integer.MIN_VALUE. Since
Integer.MIN_VALUE is a legal return value of Comparator.compareTo, a comparator which simply
applies the unary minus operator to the result of another comparator's compareTo method is not
guaranteed to represent a reverse ordering of that comparator.

Note from the Author or Editor:
Fixed in 2e

Anonymous   
Page 58
ninth bullet point

"The erasures of S and T in the definition of copy are Appendable and Readable, because S has
bound Appendable & Closeable and T has bound Readable & Closeable."

This is incorrect, S and T have been switched. S has bound Readable & Closeable. T has bound
Appendable & Closeable (see page 47). So it should read :

"The erasures of S and T in the definition of copy are Readable and Appendable, because S has
bound Readable & Closeable and T has bound Appendable & Closeable."

Note from the Author or Editor:
Fixed.

Anonymous   
Page 59
sample listing Overloaded2

Not really a technical mistake, more of an issue in editing. The text says: "However, say we change the
methods so that each appends its result to the end of the argument lis rather than returning a value.",
then the listing for Overloaded2, with two methods:
public static boolean allZero(List<Integer> list) and
public static boolean allZero(List<String> list).

However, neither of those methods:
- are changed versions of previous methods (the previous example used sum() methods that returned a sum
of integers or concatenated Strings);
- appends anything to the argument list.

Note from the Author or Editor:
Example removed from 2e.

Anonymous   
Printed
Page 86
first line of first full paragraph (after code snippet)

"unchecked cast to T()" should read "unchecked cast to T[]"

I.e., the square brackets were mis-typed as paranetheses.

Anonymous   
Printed
Page 99
second paragraph

There's an exclaimation point in the middle of the phrase "generics!for reflection"

Anonymous   
Printed
Page 137
last paragraph

"expects an arguement" should be "expects an argument"

Anonymous   
Printed
Page 152
Figure 10.1

"ConcurrenNavigableMap" should read "ConcurrentNavigableMap"

Anonymous   
Printed
Page 159
2nd last paragraph

Remove "the" from "This is particularly true for the a graphical user interface".

Anonymous   
Printed
Page 162
second full paragraph

The apostrophy in "collection's" appears as a registered trademark symbol.

Anonymous   
Printed
Page 242
last paragraph

The phrase

the platform implementations of NavigableMap.keySet do return a NavigableMap.

should read

the platform implementations of NavigableMap.keySet do return a NavigableSet.

Anonymous