Skip to content

Instantly share code, notes, and snippets.

@rayhon1014
Last active December 3, 2016 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayhon1014/7b2535983f06cc619e5decc7e8ee2a77 to your computer and use it in GitHub Desktop.
Save rayhon1014/7b2535983f06cc619e5decc7e8ee2a77 to your computer and use it in GitHub Desktop.
// ------------------------- 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