• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Trying to understand how the datatypes are determined in this reduce()

 
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tb864615.OCPJAVASE17PT.c06.079

I am trying to understand how the datatypes are determined in this reduce(). (It doesn't compile - book wants you to know why)
I understand the identity is Integer.
But shouldn't w,y,z be StringBuilder?

Based on the reduction operation, the data types of w, y, and z are Integer, while the data type of x is StringBuilder.


     
 
Saloon Keeper
Posts: 15727
368
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look carefully at the parameters of the reduce() function.

We passed in an Integer as the identity, so we can substitute Integer for U. We also know that the stream consists of StringBuilder, so we substitute StringBuilder for T.


So the accumulator is a function that accepts an Integer and a StringBuilder. That means:

And the combiner is a function that takes two Integers:

Now you can easily see why the program is wrong: w, y and z are Integer and thus lack a length() method.
 
Stephan van Hulst
Saloon Keeper
Posts: 15727
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can fix the program like this:

Here's a way of visualizing a possible execution:

Stream:["hello", "goodbye"]
After splitting:[["hello"], ["goodbye"]]
After accumulating:[5, 7]
After combining:12
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Look carefully at the parameters of the reduce() function.

So the accumulator is a function that accepts an Integer and a StringBuilder. That means:

And the combiner is a function that takes two Integers:


I see now that we must pay close attention not only to the generic types in the definition, but also to the order in which they appear, and attempt to map it to the actual arguments.
 
Stephan van Hulst
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it.
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic