Java 8 Lambdas

Errata for Java 8 Lambdas

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
Printed Page 34
Example 3-23

errata in book: public Set<String> findLongTracks2(List<Album> albums) { return .flatMap(album -> album.getTracks()) .filter( track -> track.getLength() > 60) .map(Track::getName) .collect(Collectors.toSet()); } correct would be: public Set<String> findLongTracks2(List<Album> albums) { return .flatMap(album -> album.getTracks().stream()) .filter( track -> track.getLength() > 60) .map(Track::getName) .collect(Collectors.toSet()); }

Luis  Jan 30, 2018 
PDF Page 38

In exercise 5, it looks like the numbering of subexercises is a bit messed up. Judging from the repository with the solutions, the part "x -> x +1" which is not numbered at all is exercise 5a, whereas "The lambda expression passed into forEach in the example" which is labeled as 'a.' is actually exercise 5b.

Anonymous  Sep 13, 2019 
Printed Page 52
Graph at the top

In Figure 4-5 OverridingParent class should have its own - welcome method and NOT `welcome from Parent`, and OverridingChild should have `welcome from OverridingParent` and NOT `welcome from Parent`

Koray Tugay  Dec 26, 2018 
ePub Page 80
Bottom of page

You have the following sentence: “For example, they might assume that the object has internal state and be interfaces with a single method only coincidentally.” I would suggest changing "be interfaces" to "is an interface." The sentence would then read: “For example, they might assume that the object has internal state and is an interface with a single method only coincidentally.” In any case, I don't think that "interface" can be plural, as you've written. The thing it's talking about, "object," is singular. You've made the agreement correctly with "internal state." (You didn't write "internal states"—plural.) The word "interface" should match up with the same agreement.

Mario Diana  Sep 09, 2016 
Printed Page 86
Code of Example 6-3

The following code has 3 issues. return IntStream.range(0, N) .parallel() .mapToObj(twoDiceThrows()) //Error 1 .collect(groupingBy(side -> side, //Error 2 summingDouble(n -> fraction ))); //Error 3 1. The argument to mapToObj() should be a lambda expression, not a plain method call. 2. groupingBy() gets an object as parameter and the lambda expression on the right hand side should use the (.) notation to access the side field. 3. summingDouble() again gets an object and it's field should be accessed using (.) notation. **More importantly, 'fraction' needs to be multiplied by the count of occurrences of the side** The fixed code should look something like below: return IntStream.range(0, N) .parallel() .mapToObj(n->twoDiceThrows()) .collect(groupingBy(outcome ->outcome.side, summingDouble(outcome -> fraction * outcome.n))); ----------------------------------------------------------------------------------------------- My complete working solution is below. package Streams; import java.util.Map; import java.util.Random; import; import static; import static; public class ParallelStreams { final static int N = 100000000; static Random r = new Random(); public static Map<Integer, Double> parallelDiceRolls() { double fraction = 1.0/N; return IntStream.range(0, N) .parallel() .mapToObj(n->twoDiceThrows()) .collect(groupingBy(outcome ->outcome.side, summingDouble(outcome -> fraction * outcome.n))); } public static void main(String[] args) { Map<Integer, Double> results = parallelDiceRolls(); results.forEach((x,y) -> System.out.format("%d, %f%n", x, y)); } private static DiceOutcome twoDiceThrows() { return new DiceOutcome(2 + r.nextInt(6) + r.nextInt(6)); } static class DiceOutcome { DiceOutcome(int side) {this.side = side;} public int side; int n = 1; } }

Murali Mohan  Nov 01, 2018 
ePub Page 2760
Collection Niceties

In 5-30 (code under Collection Niceties) - the alternative to creating the collector is inappropriate in this case as it will not work with parallelStream

Muhammed Shakir  Aug 17, 2017