Skip to content

Commit c17d8e5

Browse files
author
Graham Allan
committed
Add in-progress example for method references.
1 parent 8bbe022 commit c17d8e5

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ To follow the exercises:
1818
1. Internal vs External Iteration (the forEach method)
1919
2. Filtering and Collecting
2020
3. Mapping
21+
4. (In Progress) Method References
2122

2223
[More to come]
2324

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.adoptopenjdk.lambda.tutorial;
2+
3+
/*
4+
* #%L
5+
* lambda-tutorial
6+
* %%
7+
* Copyright (C) 2013 Adopt OpenJDK
8+
* %%
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as
11+
* published by the Free Software Foundation, either version 2 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public
20+
* License along with this program. If not, see
21+
* <http://www.gnu.org/licenses/gpl-2.0.html>.
22+
* #L%
23+
*/
24+
25+
import java.util.function.Consumer;
26+
27+
/**
28+
* Exercise 4 - Method References
29+
* <p>
30+
* Method references are another syntactic addition to JDK 8. They are intended to be used in lambda expressions,
31+
* preventing unnecessary boilerplate. Consider the following lambda from an earlier test:
32+
* <code>books.stream().map(b -> b.getTitle())</code>. Here the lambda does nothing more than invoke a method on each
33+
* element of the stream. This can be written alternatively as: <code>books.stream().map(Book::getTitle)</code>. Both
34+
* forms are equivalent.
35+
* </p>
36+
* <p>
37+
* The parameter list and return type when using a method reference must match the signature of the method. A mismatch
38+
* between the two will result in a compile error, just as invoking a method with parameters or return type with an
39+
* incorrect type will cause a compile error.
40+
* For example, consider the following code:
41+
* <pre>
42+
* public class Printers {
43+
* public static void print(String s) {...}
44+
* }
45+
* </pre>
46+
* Note that the <code>printNumber</code> method takes a single <code>String</code> parameter, and has a void return
47+
* type. Although declared using different type names, this is the same signature as {@link Consumer}, the functional
48+
* interface that's passed to {@link Iterable#forEach(Consumer)}. Since they match, we can use a reference to the
49+
* <code>print</code> method when invoking forEach on a list of Strings, like so:
50+
* <pre>
51+
* Arrays.asList("a", "b", "c").forEach(Printers::print)
52+
* </pre>
53+
* If we changed the signature of <code>print</code> to <code>public static void print(String s, String t)</code>
54+
* the use of the method reference would no longer compile, with an error message pointing to the mismatch in argument
55+
* lists.
56+
* </p>
57+
* <p>
58+
* There are four different kinds of methods that can be used with the method reference syntax:
59+
* <ol>
60+
* <li>Static method belonging to a particular class</li>
61+
* <li>Instance method bound to a particular object instance</li>
62+
* <li>Instance method bound to an particular class</li>
63+
* <li>Constructor belonging to a particular class</li>
64+
* </ol>
65+
* </p>
66+
* We'll discuss each in turn:
67+
* <p>
68+
* <em>Static method belonging to a particular class</em>
69+
* <br>
70+
* In the previous example, the <code>Printers::print</code> method reference is to a static method ('print') belonging
71+
* to the Printers class. Here the argument list of the lambda must match the argument list of the method, the first
72+
* argument to the lambda is the first argument passed into the method.
73+
* </p>
74+
* <em>Instance method bound to a particular object instance</em>
75+
* TODO: Carry on from here.
76+
*
77+
*/
78+
@SuppressWarnings("unchecked")
79+
public class Exercise_4_Test {
80+
81+
82+
}

0 commit comments

Comments
 (0)