• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

map() method in Streams

 
Ranch Hand
Posts: 52
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the following code work ?



I know that the map() method has the following syntax..


It takes a an argument of type Function which is a functional interface. Function requires a parameter of type <T> and returns a parameter of type <R>.
Here the isFourMinuteMile() method does not accept any argument then how can it be passed as a method reference here ?
I think that here the argument which Function is receiving is the data from the stream. Can anyone please explain this ?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arpan Ghoshal wrote:. . . Here the isFourMinuteMile() method does not accept any argument . . .

There is an argument somewhere. It't just well hidden.
When you say Runner::isFourMinuteMile, that looks like a static method call. But it isn't static, as you can see from the code above. So you have a Runner object passing through the Stream, and it has to go somewhere. So it goes on the left. Yes, rather than trying to pass it as the argument to the method, it becomes the object on which the method is called. Not isFourMinuteMile(runner) but r.isFourMinuteMile().
I shall duplicate this discussion in our Java4 forum. I shall also leave you to work out why the result is 3, not 1. By the way: if is it a four minute mile, shouldn't the field be called seconds not minutes?
 
Arpan Ghoshal
Ranch Hand
Posts: 52
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Arpan Ghoshal wrote:. . . Here the isFourMinuteMile() method does not accept any argument . . .

There is an argument somewhere. It't just well hidden.
When you say Runner::isFourMinuteMile, that looks like a static method call. But it isn't static, as you can see from the code above. So you have a Runner object passing through the Stream, and it has to go somewhere. So it goes on the left. Yes, rather than trying to pass it as the argument to the method, it becomes the object on which the method is called. Not isFourMinuteMile(runner) but r.isFourMinuteMile().
I shall duplicate this discussion in our Java4 forum. I shall also leave you to work out why the result is 3, not 1. By the way: if is it a four minute mile, shouldn't the field be called seconds not minutes?



Thanks for replying it cleared my doubts. I already suspected of this,

Arpan Ghoshal wrote:I think that here the argument which Function is receiving is the data from the stream.  


This was just a code I copied from the book of Scott and Jeanne for OCP so I don't know about the method name.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't quote the whole of previous posts. That simply makes the thread longer without adding new information,
Did you work out why you are getting 3 rather than 1?

Thanks for replying it cleared my doubts.

That's a pleasure

I already suspected of this,

Yes, the Runner object is the T; the R is the return value of the method named.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To make that visible in an easy way, try:

Note: it helps to give the Runner class a decent toString method, and the above will generate an error, about reusing a Stream, but that should be easy to fix.
 
Arpan Ghoshal
Ranch Hand
Posts: 52
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Did you work out why you are getting 3 rather than 1?



It gives three because there are three Runner Objects in the stream..
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and what type is the Stream in your original post in line 29? I don't mean runners; I mean the Stream created by the map() call in line 29.
 
Arpan Ghoshal
Ranch Hand
Posts: 52
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:. . . and what type is the Stream in your original post in line 29? I don't mean runners; I mean the Stream created by the map() call in line 29.


Here the call to map() maps the stream of runners to a stream of Booleans according to the mapping function which is the method isFourMinuteMile() in this case. Thus we obtain a stream of 3 Boolean values and count() returns 3.
 
Arpan Ghoshal
Ranch Hand
Posts: 52
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:. . . and what type is the Stream in your original post in line 29? I don't mean runners; I mean the Stream created by the map() call in line 29.


Here the call to map() maps the stream of runners to a stream of Booleans according to the mapping function which is the method isFourMinuteMile() in this case. Thus we obtain a stream of 3 Boolean values and count() returns 3.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arpan Ghoshal wrote:. . . a stream of Booleans . . . 3 Boolean values . . . .

Spot on (twice).
 
reply
    Bookmark Topic Watch Topic
  • New Topic