OnJava8-Examples/functions/arr/ArgReturnReferences.java
2015-11-03 12:00:44 -08:00

34 lines
829 B
Java

// functions/arr/ArgReturnReferences.java
// Demonstrates method references
package functions.arr;
import java.util.function.*;
class Y {
static Y create() { return new Y(); }
static void absorb(Y y) {}
static String transform1(Y y) { return "Y"; }
static String transform2(Y y, String s) {
return "Y" + " " + s;
}
}
public class ArgReturnReferences {
Supplier<Y> supply = Y::create;
Consumer<Y> consume = Y::absorb;
Function<Y, String> f1arg = Y::transform1;
BiFunction<Y, String, String> f2arg =
Y::transform2;
public static void main(String[] args) {
ArgReturnReferences arr =
new ArgReturnReferences();
Y y = arr.supply.get();
arr.consume.accept(y);
System.out.println(arr.f1arg.apply(y));
System.out.println(arr.f2arg.apply(y, "Howdy"));
}
}
/* Output:
Y
Y Howdy
*/