Errata

Modern Java Recipes

Errata for Modern Java Recipes

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
Examples 9.23 and 9.24

In examples 9.23 and 9.24, join() is called on the CompletableFuture before printing Running...
As join is a blocking call, the message is printed after the future completed instead of after it started.

Note from the Author or Editor:
After Example 9-23, following the bullet list, the text current reads "The output is 'Running...' followed by 84". That should be, "Because the call to <code>join</code> is a blocking call, the output is 84 followed by 'Running'."

David Paccoud  Sep 07, 2017  Mar 30, 2018
Examples 9.23 and 9.24

In examples 9.23 and 9.24
CompletableFuture.supplyAsync(() -> this::sleepThenReturnString)
should read
CompletableFuture.supplyAsync(this::sleepThenReturnString)

Note from the Author or Editor:
Yes, thanks for the correction. We need to drop the "() ->" exactly as you stated in both examples. The second one is no doubt a copy-and-paste error from the first. Fortunately both are correct in the GitHub repo with the source code.

David Paccoud  Sep 07, 2017  Mar 30, 2018
Page 2.2 End
At end of section 2.2 Suppliers ("Other examples from the standard library that use Suppliers include:")

At the end of Section 2.2 on Suppliers, the description of Optional.orElseThrow is not correct.

Text in book:
"The orElseThrow method in Optional, which takes a Supplier<X extends Exception>. The Supplier is only executed if an exception occurs."


The javadocs describes the method as :

<X extends Throwable> T orElseThrow​(Supplier<? extends X> exceptionSupplier)

If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.






Note from the Author or Editor:
Wow, I re-read that four times and didn't see the error, until I suddenly did. Good catch (no pun intended).

Patrick Torre  Oct 09, 2021 
PDF
Page 14
System.out.println line

Arrays.toList(names)" - call should be

Arrays.asList(names)

Note from the Author or Editor:
Yes, that's a typo. Change the word toList in the code to asList, as suggested.

Detlef Boehm  Jan 19, 2018  Mar 30, 2018
PDF
Page 17
3rd paragraph from bottom

Confused about what the equals method does in Comparator. Says that two objects which equal each other should be equal to a Comparator. Comparator documentation says it refers to two Comparator objects which compare their arguments in the same way.

Note from the Author or Editor:
Replace the paragraph starting with "As it turns out..." with the following:

"What is special here is that the `equals` method shown is from `Object`, and therefore already has a default implementation. The detailed documentation says that for performance reasons you can supply your own `equals` method that satisfies the same contract, but that "it is always safe _not_ (emphasis in original) to override" this method."

The last paragraph ("The rules for functional interfaces...") should stay as written.

Campbell Ritchie  Sep 18, 2017  Mar 30, 2018
PDF
Page 21
Example 1-27. Using default methods

you cannot use the default method from Collection interface

default boolean removeIf(Predicate<? super E> filter)

on a list, created by

List<Integer> nums = Arrays.asList(3, 1, 4, 1, 5, 9);

You need to create the list by

List<Integer> nums = new ArrayList<>(Arrays.asList(3, 1, 4, 1, 5, 9));

When the predicate in

boolean removed = nums.removeIf(n -> n <= 0);

gets true by adding a Interger -2 you get an

java.lang.UnsupportedOperationException otherwise ...

Note from the Author or Editor:
Please change the line:
List<Integer> nums = Arrays.asList(3, 1, 4, 1, 5, 9);
to
List<Integer> nums = new ArrayList<>();
nums.add(-3); nums.add(1); nums.add(4);
nums.add(-1); nums.add(5); nums.add(9);

Detlef Boehm  Jan 19, 2018  Mar 30, 2018
PDF
Page 32
Example 2-6

Method of Predicate class shown as isEquals. Should read isEqual without last s.

Note from the Author or Editor:
Method should be "isEqual" without a trailing "s"

Campbell Ritchie  Sep 18, 2017  Mar 30, 2018
Printed
Page 32
Example 2-8

Example 2-8 uses String s as a parameter. The .filter() tries to use s as the iteration variable for strings within the Arrays.stream(). Two differently named variables are needed.

Note from the Author or Editor:
Correct. In the code in example 2-8, change the "filter" line to:

.filter(str -> str.startsWith(s))

Chris Pieper  Dec 01, 2017  Mar 30, 2018
Printed
Page 37
Table 2-3

The single abstract method for BiFunction is incorrect in the table
It should be: R apply(T t, U u)

Note from the Author or Editor:
Yes, it's a typo. The signature in the book starts with "void" and it should be "R", exactly as the error stated.

Chris Davidson  Sep 05, 2017  Mar 30, 2018
PDF
Page 42
Right in the middle

It says
"Their method signatures from IntStream are (LongStream is similar) . . ."
but shows LongStream methods too. Would read better as
"Their method signatures from IntStream and LongStream are . . . "

Note from the Author or Editor:
The text should say "the methods from IntStream and LongStream are"

Campbell Ritchie  Sep 18, 2017  Mar 30, 2018
PDF
Page 44
Very bottom

It says Integer constructor is used but shows valueOf in the preceding example.

Note from the Author or Editor:
Instead of "uses the Integer constructor", should say "uses the static valueOf method from the Integer class"

Campbell Ritchie  Sep 18, 2017  Mar 30, 2018
PDF
Page 45
Example 3-13

You will be fed up to the teeth with the sight of me by now.
It says toArray returns Object[], but the methods of IntStream LongStream and DoubleStream have a different return type. Only Stream<T> appears to have the toArray(T[]::new) method.

Note from the Author or Editor:
Chose a bad example here. Easiest fix is in Example 3-13, delete the lines "// or" and the "int[] intArray = ... toArray(int[]::new)", as well as the following paragraph ("The first demo...").

The end result is that Example 3-13 is just one line, followed by the last paragraph ("The fact that...").

Campbell Ritchie  Sep 18, 2017  Mar 30, 2018
PDF
Page 66
End of 4-6

This sentence does not parse:

"Each returns a sequential, ordered stream that starts and the
first argument and increments by one after that."

I believe you mean:

"Each returns a sequential, ordered stream that starts on the
first argument and increments by one after that."

eldavojohn  Apr 03, 2017  Aug 04, 2017
Printed
Page 74
Box on BigInteger and Primes

If certainty is 2 then the probability is 0.75 and not 0.5. And so on.

Note from the Author or Editor:
This is correct. Change it to "a certainty of 1 implies a probability of 0.5, a certainty of 2 implies 0.75, 3 implies 0.875, and so on."

Gaurav Khanna  Feb 21, 2018  Mar 30, 2018
PDF
Page 118
Paragraph 2 line 5 and 3rd line from the bottom

The same interface is called ActionEventListener at the top of the page and ActionListener at the bottom of the page. I think ActionListener is correct

Note from the Author or Editor:
Yes, it should be ActionListener.

Campbell Ritchie  Oct 11, 2017  Mar 30, 2018
PDF
Page 125
Ex 5:10

No mention of case-sensitivity: shouldn't there be a toLowerCase() call somewhere. In ex5:11 is says NSA=2 agent=1: surely it should be the other way round?

Hope to ask question about computeIfPresent on Coderanch within the next 24 hours.

Note from the Author or Editor:
This is correct. I copied the data before it was changed. Need the last sentence in the example to be "NSA agent says...". Then the result will be NSA=2, agent=2, joke=1.

Campbell Ritchie  Oct 12, 2017  Mar 30, 2018
PDF
Page 135
ex 5-24 line marked 2 at the end

add2mult3 spelt with double m add2mmult3

I have had another look at computeIfAbsent and I think I have solved the query I had yesterday.

Note from the Author or Editor:
yes, that should be just `add2mult3` (the second m should be deleted)

madcyclist  Oct 13, 2017  Mar 30, 2018
Printed
Page 148
Example 6.1

Example 6.1

AtomicInteger counter = new AtomicInteger();
Optional<AtomicInteger> opt = Optional.ofNullable(counter);

System.out.println(optional); // Optional[0]

counter.incrementAndGet(); 1
System.out.println(optional); // Optional[1]

optional.get().incrementAndGet(); 2
System.out.println(optional); // Optional[2]

optional = Optional.ofNullable(new AtomicInteger()); 3

==========================================================
2nd line of code. Should "opt" be "optional" ?

Optional<AtomicInteger> optional = Optional.ofNullable(counter);

Note from the Author or Editor:
Yes, "opt" should have been "optional" on that line.

cggalloway  Oct 19, 2018 
PDF
Page 217
Bullet point No 2 in upper part of page

It says submit Callable<Integer> but the code seems to be submitting a Callable<Long>

Note from the Author or Editor:
That's correct. In bullet 2, change "Callable<Integer>" to "Callable<Long>"

Campbell Ritchie  Dec 06, 2017  Mar 30, 2018
PDF
Page 255
Near top

On two occasions it says seek numbers over 70 but the following code has 90 in. It also says seek those values in random ints between 0 and 50 whereas the code has 0, 100. The second example has 90 in both places

Note from the Author or Editor:
Ugh. Yes, this is correct. The first line should say "generating 50 random integers between 0 and 100, sorting them in descending order, and returning only those whose value is greater than 90".
Then the caption for Example 10-19 should say "Taking ints above 90".
Finally, bullet 3 should say "Split the stream and return the values greater than 90".

Campbell Ritchie  Dec 07, 2017  Mar 30, 2018
PDF
Page 275
Last line

Block off the old chip?
I didn't know that JB coined the PECS mnemonic/acronym in Effective Java, but when I saw PECS I recognised it. But you have him down as Block not Bloch.

Note from the Author or Editor:
This should definitely be "Bloch". I thought that's what I added, but perhaps it got changed during an auto-correct pass.

Campbell Ritchie  Dec 08, 2017  Mar 30, 2018
PDF
Page 277
About six lines down

Several instances of "maximum" and one "minimum" has slipped in.

Note from the Author or Editor:
Correct. Replace the word "minimum" in the sentence "The method wraps the minimum..." with the word "maximum".

Campbell Ritchie  Dec 08, 2017  Mar 30, 2018