函数式接口
函数式接口
()->{}
这样的语法本质上还是继承并且实现了一个接口。@FunctionalInterface
注解。举个简单的例子,
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
我们不能使用以下类型的方法来声明一个功能接口:默认方法、静态方法、继承自
package java.util;
@FunctionalInterface
public interface Comparator<T> {
// An abstract method declared in the functional interface
int compare(T o1, T o2);
// Re-declaration of the equals() method in the Object class
boolean equals(Object obj);
...
}
Builtin Functional Interface | 函数式接口
Consumer
Consumer<String> c = (x) -> System.out.println(x.toLowerCase());
c.accept("Java2s.com");
public class Main {
public static void main(String[] args) {
int x = 99;
Consumer<Integer> myConsumer = (y) ->
{
System.out.println("x = " + x); // Statement A
System.out.println("y = " + y);
};
myConsumer.accept(x);
}
}
public class Main {
public static void main(String[] args) {
List<Student> students = Arrays.asList(
new Student("John", 3),
new Student("Mark", 4)
);
acceptAllEmployee(students, e -> System.out.println(e.name));
acceptAllEmployee(students, e -> {
e.gpa *= 1.5;
});
acceptAllEmployee(students, e -> System.out.println(e.name + ": " + e.gpa));
}
public static void acceptAllEmployee(List<Student> student, Consumer<Student> printer) {
for (Student e : student) {
printer.accept(e);
}
}
}
class Student {
public String name;
public double gpa;
Student(String name, double g) {
this.name = name;
this.gpa = g;
}
}
DoubleConsumer d = (x) -> System.out.println(x*x);
d.accept(0.23);
BiConsumer<String, String> biConsumer = (x, y) -> {
System.out.println(x);
System.out.println(y);
};
biConsumer.accept("java2s.com", " tutorials");
biConsumer.andThen(biConsumer).accept("java2s.com", " tutorial");
Comparators
Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName);
Person p1 = new Person("John", "Doe");
Person p2 = new Person("Alice", "Wonderland");
comparator.compare(p1, p2); // > 0
comparator.reversed().compare(p1, p2); // < 0
Function
BiFunction<String, String,String> bi = (x, y) -> {
return x + y;
};
System.out.println(bi.apply("java2s.com", " tutorial"));
// BiFunction 作为参数
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
String result = calculator.calc((a, b) -> ": " + (a * b),3, 5);
System.out.println(result);
}
}
class Calculator {
public String calc(BiFunction<Integer, Integer, String> bi, Integer i1, Integer i2) {
return bi.apply(i1, i2);
}
}
// 传入 Stream 中
List<Integer> _numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Function<Integer, Integer> lambda = value -> value * 2;
List<Integer> doubled = _numbers.stream()
.map(lambda)
.collect(java.util.stream.Collectors.toList());
System.out.println(doubled);
// 传入方法引用
public class Main {
public static void main(String[] args) {
List<Double> numbers = Arrays.asList(1D, 25D, 100D);
System.out.println(transformNumbers(numbers, Math::sqrt));
}
private static List<String> transformNumbers(List<Double> numbers, Function<Double, Double> fx) {
List<String> appliedNumbers = new ArrayList<>();
for (Double n : numbers) {
appliedNumbers.add(String.valueOf(fx.apply(n)));
}
return appliedNumbers;
}
}
DoubleFunction<String> df = (d) -> d +" is now a string";
System.out.println(df.apply(0.5));
Predicate
Predicate<String> predicate = (s) -> s.length() > 0;
predicate.test("foo"); // true
predicate.negate().test("foo"); // false
Predicate<Boolean> nonNull = Objects::nonNull;
Predicate<Boolean> isNull = Objects::isNull;
Predicate<String> isEmpty = String::isEmpty;
Predicate<String> isNotEmpty = isEmpty.negate();
BiPredicate<Integer, Integer> bi = (x, y) -> x > y;
System.out.println(bi.test(2, 3));
public class Main {
public static void main(String[] args) {
boolean result = compare((a, b) -> a / 2 == b, 10, 5);
System.out.println("Compare result: " + result);
}
public static boolean compare(BiPredicate<Integer, Integer> bi, Integer i1, Integer i2) {
return bi.test(i1, i2);
}
}
Operator
BinaryOperator<Integer> adder = (n1, n2) -> n1 + n2;
System.out.println(adder.apply(3, 4));
DoubleBinaryOperator d = (x,y) -> x*y;
System.out.println(d.applyAsDouble(0.23, 0.45));
Supplier
Supplier<String> i = ()-> "java2s.com";
System.out.println(i.get());
下面的代码显示了如何传递
public class Main {
public static SunPower produce(Supplier<SunPower> supp) {
return supp.get();
}
public static void main(String[] args) {
SunPower power = new SunPower();
SunPower p1 = produce(() -> power);
SunPower p2 = produce(() -> power);
System.out.println("Check the same object? " + Objects.equals(p1, p2));
}
}
class SunPower {
public SunPower() {
System.out.println("Sun Power initialized..");
}
}
下面的代码显示了如何使用构造函数作为
public class Main {
public static void main(String[] args) {
System.out.println(maker(Employee::new));
}
private static Employee maker(Supplier<Employee> fx) {
return fx.get();
}
}
class Employee {
@Override
public String toString() {
return "A EMPLOYEE";
}
}
下面的代码显示了如何通过方法引用将用户定义的函数分配给
public class Main {
public static void main(String[] args) {
Supplier<Student> studentGenerator = Main::employeeMaker;
for (int i = 0; i < 10; i++) {
System.out.println("#" + i + ": " + studentGenerator.get());
}
}
public static Student employeeMaker() {
return new Student("A",2);
}
}
class Student {
public String name;
public double gpa;
Student(String name, double g) {
this.name = name;
this.gpa = g;
}
@Override
public String toString() {
return name + ": " + gpa;
}
}
BooleanSupplier bs = () -> true;
System.out.println(bs.getAsBoolean());
int x = 0, y= 1;
bs = () -> x > y;
System.out.println(bs.getAsBoolean());