JDK1.8学习笔记2-函数式接口

1Function函数式接口

1.1基本用法

抽象接口

  • apply—将此函数应用于给定的参数

默认实现接口

  • compose—将compose传入函数的返参作为当前函数的入参
  • andThen—将当前函数的返参作为andThen的入参

1.2 compose方法

public class FunctionTest {
    public static void main(String[] args) {
        FunctionTest functionTest = new FunctionTest();

        System.out.println(functionTest.compute(1, value ->  2 * value));
        System.out.println(functionTest.compute(2, value -> 5 + value));
        System.out.println(functionTest.compute(3, value -> value * value));
        System.out.println(functionTest.convert(4, value -> value + " convert"));
        //
  		Function<Integer, Integer> function = value -> 2 * value;
        System.out.println(functionTest.compute(1, function));
    }

    public int compute(int a, Function<Integer, Integer> function){
        int result = function.apply(a);
        return result;
    }

    public String convert(int a, Function<Integer, String> function){
        return function.apply(a);
    }
}

高阶函数:—接收一个函数作为参数,或者将一个函数作为返回值的函数

1.3 compose方法

示例

public class FunctionTest2 {
    public int composeTest(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
        return function1.compose(function2).apply(a);
    }

    public static void main(String[] args) {
        FunctionTest2 functionTest2 = new FunctionTest2();
        System.out.println(functionTest2.composeTest(2, value -> value * 3, value -> value * value));
    }
}

输出结果:

12

先执行function2函数的执行作为function1的参数
2*2*3=12

1.4 andThen方法

示例

public class FunctionTest2 {
    public int andThenThes(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
        return function1.andThen(function2).apply(a);
    }

    public static void main(String[] args) {
        FunctionTest2 functionTest2 = new FunctionTest2();
        System.out.println(functionTest2.andThenThes(2, value -> value * 3, value -> value * value));
    }
}

输出结果:

36

先执行function1函数的执行结果作为function2的参数
(2*3)*(2*3)=36


2 BiFunction双参数函数式接口

抽象方法

  • apply—将此函数应用于给定的两个参数
    默认实现接口
  • andThen—将当前函数两个参数执行后的返参作为andThen的入参
  • BiFunctionFunction接口类似,不同点在于前者接收两个参数,后者只接收一个参数

示例

public class FunctionTest2 {
    public int bitest(int a, int b, BiFunction<Integer, Integer, Integer> biFunction){
        return biFunction.apply(a, b);
    }
    public int bitAndThenTest(int a, int b, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function){
        return biFunction.andThen(function).apply(a, b);
    }
    
    public static void main(String[] args) {
        FunctionTest2 functionTest2 = new FunctionTest2();

 		System.out.println(functionTest2.bitest(1, 2, (value1, value2) -> value1 + value2));
        System.out.println(functionTest2.bitest(1, 2, (value1, value2) -> value1 - value2));
        System.out.println(functionTest2.bitest(1, 2, (value1, value2) -> value1 * value2));
        System.out.println(functionTest2.bitest(1, 2, (value1, value2) -> value1 / value2));    
        System.out.println(functionTest2.bitAndThenTest(2, 3, (value1, value2) -> value1 + value2, value -> value * value));

        }
}

输出结果

3
-1
2
0
25


3 Predicate断言函数式接口

抽象接口

  • test()—断言传入参数是否为真,抽象接口

默认实现

  • and()—两个断言进行与操作,默认实现
  • negate()—取当前断言的相关结果,及!test(),默认实现
  • or()—两个断言进行或操作,默认实现

示例

public class PredicateTest {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 10);

        PredicateTest test = new PredicateTest();
        test.conditionFilter(list, integer -> integer % 2 == 0);
        System.out.println("-----");
        test.conditionFilter(list, integer -> integer % 2 == 1);

        System.out.println("-----");
        test.conditionFilter(list, integer -> integer > 5);

        System.out.println("-----");
        test.conditionFilter(list, integer -> true);

        System.out.println("-----");
        test.conditionFilter(list, integer -> false);
    }

    public void conditionFilter(List<Integer> list, Predicate<Integer> predicate) {
        list.forEach(integer -> {
            if (predicate.test(integer)) {
                System.out.println(integer);
            }
        });
    }
}

执行结果

2
4
6
10
+++++
1
3
5
7
9
+++++
6
7
9
10
+++++
1
2
3
4
5
6
7
9
10
+++++

4 Supplier函数式接口

不接受参数,同时返回一个结果

4.1 示例1

public class SupplierTest1 {
    public static void main(String[] args) {
        Supplier<String> supplier = () -> "hello world";
        System.out.println(supplier.get());
    }
}

执行结果

hello world

4.1 示例2

public class SupplierTest2 {
    public static void main(String[] args) {

        Supplier<Student> supplier1 = () -> new Student();
        System.out.println(supplier1.get().getName());

        System.out.println("++++++++");

        Supplier<String> supplier2 =  Student::getStr;
        System.out.println(supplier2.get());
    }
}

public class Student {

    private String name;

    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student() {
        name = "zhangsan";
        age = 20;
    }

    private static String str = "hello world";

    public static String getStr() {
        return str;
    }

}

执行结果

zhangsan
++++++++
hello world

5 BinaryOperator函数式接口

继承自BiFunction接口,不同点在于BinaryOperator函数式接口的两个入参与返参类型一致

public class BinaryOperatorTest {

    public static void main(String[] args) {
        BinaryOperatorTest test = new BinaryOperatorTest();
		
        System.out.println(test.compute(5, 3, (a, b) -> a + b));
        System.out.println(test.compute(5, 3, (a, b) -> a - b));

        System.out.println(test.getMin(5, 8, Integer::compareTo));

        //比较长度
        System.out.println(test.getShort("hello123", "world", (a, b) -> a.length() - b.length()));

        //比较首字母
        System.out.println(test.getShort("hello123", "world", (a, b) -> a.charAt(0) - b.charAt(0)));

    }
    public Integer compute(Integer a, Integer b, BinaryOperator<Integer> operator) {
        return operator.apply(a, b);
    }

    public Integer getMin(Integer a, Integer b, Comparator<Integer> comparator){
        return BinaryOperator.minBy(comparator).apply(a, b);
    }

    public String getShort(String a, String b, Comparator<String> comparator){
        return BinaryOperator.minBy(comparator).apply(a, b);
    }
}

执行结果

8
2
5
world
hello123

你可能感兴趣的:(jdk1.8)