Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Introduction to Functional Programming Meet Streams Transforming With Map

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Method from reference has different signature than the method in functional interface that we override

private static List<String> getCaptionsStream(List<Job> jobs) { return jobs.stream() .filter(App::isJuniorJob) .map(Job::getCaption)
.limit(3) .collect(Collectors.toList()); }

Here we use Job::getCaption method reference. Referenced method has this singature - public String getCaption() and the method overriden in functional interface has this signature - R apply(T t) Why isn't that a problem that our referenced method doesn't have parameter?

3 Answers

The Job::getCaption method reference is not 'overriding' apply. It's a method call to the stream.map() method which can take the Job::getCaption since Job::getCaption is what we call in Java: a supplier. I.E. A function that takes no arguments and returns a result.

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Lukas Dahlberg 1.Acutally, it does override apply method in Function functional interface - look at the screenshot https://ibb.co/2yqKp16 2.Method signature of the overriden method is not a supplier because as you said supplier takes no arguments and apply method does - R apply(T t);

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Craig Dennis Could you please help? I tried to find the answer for a while now but failed miserably.

Hi Andre Kucharzyk , a little late, but this is what is referred to in the video as Method Reference Inference. The getCaption method is not static and can only be invoked on an instance of a Job. It's using this.getCompany(), only that the keyword "this" can be omitted.

Cheers, Toby