double calculate(int a);
default double sqrt(int a) {
return Math.sqrt(a);
}
}
Formula formula = new Formula() {
@Override
public double calculate(int a) {
return sqrt(a * 100);
}
};
formula.calculate(100); // 100.0
formula.sqrt(16);
List
Collections.sort(names, new Comparator
@Override
public int compare(String a, String b) {
return b.compareTo(a);
}
});
Collections.sort(names, (String a, String b) -> {
return b.compareTo(a);
});
Collections.sort(names, (a, b) -> b.compareTo(a));
@FunctionalInterface
interface Converter
T convert(F from);
}
Converter
Integer converted = converter.convert("123");
System.out.println(converted); // 123
Converter
Integer converted = converter.convert("123");
System.out.println(converted); // 123
class Something {
String startsWith(String s) {
return String.valueOf(s.charAt(0));
}
}
Something something = new Something();
Converter
String converted = converter.convert("Java");
System.out.println(converted); // "J"
class Person {
String firstName;
String lastName;
Person() {}
Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
interface PersonFactory
{
P create(String firstName, String lastName);}
PersonFactory
Person person = personFactory.create("Peter", "Parker")
http://winterbe.com/posts/2014/03/16/java-8-tutorial/
No comments:
Post a Comment