• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sybex OCP Java 8 Study Guide: p220 query (possible erratum?)  RSS feed

 
E Byrne
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne Boyarsky & Scott Selikoff

A small query about the second statement in the final paragraph of p220:

“The groupingBy() collector tells collect() that it should group all of the elements of the stream into lists, organizing them by the function provided. This makes the keys in the map the function value and the values the function results.”

Would it not be more a case of:
This makes the keys in the map the function results (returns from Function’s apply(...) method), while the values are lists of function argument giving the same result.

My wording is not great due to the complexity of this situation – there’s probably a better way to express it, or maybe I’ve misunderstood the intention of the original statement, or how the code works!

EB
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, let's have a look at the Collectors#groupingBy() method.
It tells you that the “K”s are the results returned from the apply() method. It says the
values are Lists containing the input elements which map to the associated key under the classification function.
I see what you mean about the wording. Now let's have a look at Function. That has one abstract method, apply(), which takes any of the elements handled by the Stream and produces a “R” (probably short for Result) from it. That “R” will constitute one of the “K”s in the resultant Map; the associated “V” is a List to which the element sent to the apply() method is added. I think I would prefer your wording myself, but don&apos't call something a return from a method. You should only call something a return when it is in the form of money (or some other profit).
I would word it that the “K”s are the results from applying Function#apply() and the “V”s are Lists of the elements which produced that result from Function#apply().
 
E Byrne
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Well, let's have a look at the Collectors#groupingBy() method.
It tells you that the “K”s are the results returned from the apply() method. It says the
values are Lists containing the input elements which map to the associated key under the classification function.
I see what you mean about the wording. Now let's have a look at Function. That has one abstract method, apply(), which takes any of the elements handled by the Stream and produces a “R” (probably short for Result) from it. That “R” will constitute one of the “K”s in the resultant Map; the associated “V” is a List to which the element sent to the apply() method is added. I think I would prefer your wording myself, but don&apos't call something a return from a method. You should only call something a return when it is in the form of money (or some other profit).
I would word it that the “K”s are the results from applying Function#apply() and the “V”s are Lists of the elements which produced that result from Function#apply().


Thanks, Campbell

(Re my rather sloppy use of the word “return”: I suppose I would have called it “return value” (as per the title of Table 4.11’s last column), but wanted to avoid the word “value” for fear of confusing it with the Map’s “value” parameter!)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!