Last active
December 3, 2016 06:11
-
-
Save rayhon1014/7b2535983f06cc619e5decc7e8ee2a77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ------------------------- define functional interface ------------------------------ | |
@FunctionalInterface | |
interface Converter<F, T> { | |
T convert(F from); | |
} | |
// ------------------------- use it in the code --------------------------------------- | |
//pass static method as reference | |
Converter<String, Integer> converter = Integer::valueOf; | |
Integer converted = converter.convert("123"); //convert string to Integer | |
System.out.println(converted); // 123 | |
class Something { | |
String startsWith(String s) { | |
return String.valueOf(s.charAt(0)); | |
} | |
} | |
Something something = new Something(); | |
//pass instance method as reference | |
Converter<String, String> converter = something::startsWith; | |
String converted = converter.convert("Java"); //just print out the first character | |
System.out.println(converted); // "J" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment