OnJava8-Examples/functions/MethodReferences.java

35 lines
704 B
Java
Raw Normal View History

2015-11-03 12:00:44 -08:00
// functions/MethodReferences.java
2015-06-15 17:47:35 -07:00
import java.util.*;
public class MethodReferences {
static class Description {
String describe;
public Description(String desc) {
describe = desc;
}
public void show() { System.out.println(describe); }
}
public static void main(String[] args) {
List<String> keys = Arrays.asList(
"Every", "Good", "Boy", "Deserves", "Fudge");
keys.forEach(System.out::println); // (1)
List<Description> descriptions = new ArrayList<>();
for(String k : keys)
descriptions.add(new Description(k));
descriptions.forEach(Description::show); // (2)
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Every
Good
Boy
Deserves
Fudge
Every
Good
Boy
Deserves
Fudge
2015-09-07 11:44:36 -06:00
*/