java 8 内置函数接口的使用

使用Java 编程,总会遇到很多函数接口,但Java 开发工具包(JDK)提供的一组核心函数接口会频繁出现,如图



 每个函数Demo如下:

Function<T,R>              R apply(T t);                    

Function<String, String> function = x -> x.toUpperCase();
Function<String, String> function2 = x -> x.toLowerCase();
convertString(function);// prints STRANGE
convertString(function2);// prints strange
public static void convertString(Function<String, String> function){
    System.out.println(function.apply("StRaNgE"));
}

 Supplier<T>                  T get(); 

Supplier<String> supplier1 = () -> "String1";
Supplier<String> supplier2 = () -> "String2";
printSuppliedString(supplier1);
printSuppliedString(supplier2);
public static void printSuppliedString(Supplier<String> supplier){
    System.out.println(supplier.get());
}

 Consumer<T>           void accept(T t);

Consumer<String$gt function = x -> System.out.println(x);
Consumer<String$gt function2 = x -> System.out.println(x.toLowerCase());
consumeString(function, "StringA");// prints StringA
consumeString(function2,"StringA");// prints stringa
public static void consumeString(Consumer<String> consumer, String x) {
   consumer.accept(x);
}

 interface Predicate<T>     boolean test(T t);

Predicate<Double> function = x -> x > 10;
Predicate<Double> function2 = x -> x < -10;
System.out.println(function.test(new Double(9)));// prints false
System.out.println(function2.test(new Double(-20)));// prints true
public static void testValue(Predicate&ltDouble&gt predicate, Double d){
   predicate.test(d);
}
   

 

IntConsumer	void accept(int value);	This function accepts a primitive int and does not
 return any value

 

BiFunction<T, U, R>	R apply(T t, U u);	This function accepts two arguments and 
returns a value.
 
BinaryOperator<T> extends BiFunction<T,T,T>	R apply(T t, U u);	This function is a 
special case of BiFunction where both the input parameters and the return is of the same type.
 


Method References

To increase readability java8 has come up with method references. You can access a method (lambda expression) using the :: notation. The only condition that the methods need to follow is that they should be assignable to any FunctionalInterface as described above. There are four kinds of method references, we look at them in below.

 

 

public class Example {
 
    public int add(int a, int b) {
        return a + b;
    }
 
    public static int mul(int a, int b) {
        return a * b;
    }
 
    public String lower(String a) {
        return a.toLowerCase();
    }
 
    public void printDate(Date date) {
        System.out.println(date);
    }
 
    public void oper(IntBinaryOperator operator, int a, int b) {
        System.out.println(operator.applyAsInt(a, b));
    }
 
    public void operS(Function<String, String> stringOperator, String a) {
        System.out.println(stringOperator.apply(a));
    }
 
    public GregorianCalendar operC(Supplier<GregorianCalendar> supplier) {
        return supplier.get();
    }
 
}      

 

 

Lambda Expressions - Composition

Composition allows applying lambda expressions one after another. There are two methods:

  • Function compose(Function before) - The before function is applied first and then the calling function
  • Function andThen(Function after) - The after function is applied after the calling function

Lets look at an example. This example creates a lambda expression for the Math functions. We can then apply one math function after another by using the
compose
and
andThen
methods. We can create a exp(log(sin)) or log(exp(sin)) etc .

 

import java.util.function.Function;
 
public class ExampleCompose {
 
    public static void main(String[] args) {
        ExampleCompose ex = new ExampleCompose();
        Function<Double , Double> sin = d -> ex.sin(d);
        Function<Double , Double> log = d -> ex.log(d);
        Function<Double , Double> exp = d -> ex.exp(d);
        ExampleCompose compose = new ExampleCompose();
        System.out.println(compose.calculate(sin.compose(log), 0.8));
        // prints log:sin:-0.22
        System.out.println(compose.calculate(sin.andThen(log), 0.8));
        // prints sin:log:-0.33
        System.out.println(compose.calculate(sin.compose(log).andThen(exp), 0.8));
        //log:sin:exp:0.80
        System.out.println(compose.calculate(sin.compose(log).compose(exp), 0.8));
        //exp:log:sin:0.71
        System.out.println(compose.calculate(sin.andThen(log).compose(exp), 0.8));
        //exp:sin:log:-0.23
        System.out.println(compose.calculate(sin.andThen(log).andThen(exp), 0.8));
        //sin:log:exp:0.71
 
    }
 
    public Double calculate(Function<Double , Double> operator, Double d) {
        return operator.apply(d);
    }
 
    public Double sin(Double d) {
        System.out.print("sin:");
        return Math.sin(d);
    }
 
    public Double log(Double d) {
        System.out.print("log:");
        return Math.log(d);
    }
 
    public Double exp(Double d) {
        System.out.print("exp:");
        return Math.exp(d);
    }
 
}

 

猜你喜欢

转载自840536410.iteye.com/blog/2346751