Java 函数式编程 使用

java.util.function包下有常见的函数式接口。还有一些根据基本类型封装好的接口,如IntFunction。

接口描述
Consumer接受一个输入参数,不返回结果
Function<T,R>接受一个输入参数,返回一个结果
Predicate接受一个输入参数,返回true或false
Supplier无参数,返回一个结果
BiConsumer<T,U>接受两个输入参数,不返回结果
BiFunction<T,U,R>接受两个输入参数,返回一个结果
BiPredicate <T,U>接受两个输入参数,返回true或false

1. Consumer

接受一个输入参数,不返回结果。

@Test
public void consumerTest() {
    // 接受Integer
    Consumer<Integer> consumer = x -> {
        x += 1;
        System.out.println(x);
    };
    consumer.accept(10);

    // 接受Integer
    IntConsumer intConsumer = x -> {
        x += 1;
        System.out.println(x);
    };
    intConsumer.accept(10);
}

1.1 BiConsumer

接受两个输入参数,不返回结果.

@Test
public void biConsumerTest() {
    BiConsumer<Integer, Double> biConsumer = (a, b) -> {
        System.out.println("Integer " + a + " Double " + b);
    };
    biConsumer.accept(1, 2.0);
}

2. Function

接受一个输入参数,返回一个结果。

@Test
public void functionTest1() {
    // 接受Integer,返回Integer
    Function<Integer, Integer> function = x -> x + x;
    System.out.println(function.apply(1));

	// 接受Integer,并返回Double
    IntFunction<Double> intFunction = x -> x + 0.1;
    System.out.println(intFunction.apply(1));
}

函数式接口的注解@FunctionalInterface是可选的。

作用:接口中如果有多个抽象方法则会产生编译期错误。即使用了该注解,就只能有一个抽象方法。

@FunctionalInterface
interface FunctionTest2 {
    int run(int a);
}

@Test
public void functionTest2() {
    FunctionTest2 function = x -> x + x;
    System.out.println(function.run(1));
}

函数组合,保证输入和输出类型相对应。

@Test
public void functionTest3() {
    Function<Integer, Integer> function1 = x -> {
        System.out.println("1: " + x);
        return x + x;
    };
    Function<Integer, Integer> function2 = x -> {
        System.out.println("2: " + x);
        return x + x;
    };
    Function<Integer, Integer> function3 = x -> {
        System.out.println("3: " + x);
        return x + x;
    };
    
    // 先执行function3,再function1,最后function2
    Function<Integer, Integer> function4 = function1.compose(function3).andThen(function2);
    System.out.println(function4.apply(1));
    /**
     * 执行结果
     * 3: 1
     * 1: 2
     * 2: 4
     * 8
     */
}

@Test
public void functionTest4() {
    Function<Integer, Float> function1 = x -> {
        System.out.println("1: " + x);
        return x + 0.1f;
    };
    Function<Float, Double> function2 = x -> {
        System.out.println("2: " + x);
        return x + 0.1;
    };
    
    Function<Integer, Double> function3 = function1.andThen(function2);
    System.out.println(function3.apply(1));
    /**
     * 执行结果
     * 1: 1
     * 2: 1.1
     * 1.200000023841858
     */
}

2.1 UnaryOperator

继承自Function,输入和输出返回类型相同。

@Test
public void unaryOperatorTest() {
    // 接受Integer,返回Integer
    UnaryOperator<Integer> unaryOperator = x -> {
        System.out.println(x);
        return x + x;
    };
    System.out.println(unaryOperator.apply(1));
	
	// 接受Integer,返回Integer
    IntUnaryOperator intUnaryOperator = x -> {
        System.out.println(x);
        return x + x;
    };
    System.out.println(intUnaryOperator.applyAsInt(1));
}

2.2 BiFunction

接受两个输入参数,返回一个结果。

@Test
public void biFunctionTest() {
    // 接受Integer,Double,返回Double
    BiFunction<Integer, Double, Double> biFunction = (a, b) -> {
        System.out.println("Integer " + a + " Double " + b);
        return a + b;
    };
    System.out.println(biFunction.apply(1,2.0));

    // 接受Integer,Double,返回Double
    ToDoubleBiFunction<Integer, Double> toDoubleBiFunction = (a, b) -> {
        System.out.println("Integer " + a + " Double " + b);
        return a + b;
    };
    System.out.println(toDoubleBiFunction.applyAsDouble(1,2.0));
}
2.2.1 BinaryOperator

继承自BiFunction,两个输入类型和返回类型相同。

@Test
public void binaryOperatorTest() {
    BinaryOperator<String> binaryOperator1 = (a, b) -> a + b;
    System.out.println(binaryOperator1.apply("abc", " c"));

    IntBinaryOperator intBinaryOperator = (a, b) -> a + b;
    System.out.println(intBinaryOperator.applyAsInt(1,2));
}

3. Predicate

接受一个输入参数,返回true或false。

@Test
public void predicateTest1() {
    Predicate<String> p1 = s -> s.contains("a");
    System.out.println(p1.test("abc"));
    System.out.println(p1.test("bc"));
}

函数组合

@Test
public void predicateTest2() {
    Predicate<String> p1 = s -> s.contains("a");
    Predicate<String> p2 = s -> s.length() < 3;
    Predicate<String> p3 = s -> s.contains("b");
    // 不包含a并且长度小于3 或者 包含b 或者等于fff
    Predicate<String> p4 = p1.negate().and(p2).or(p3).or(Predicate.isEqual("fff"));
    Stream.of("aa","bbbbb","cc","ddddd","fff")
            .filter(p4)
            .forEach(System.out::println);
    /**
      * 执行结果
      * bbbbb
      * cc
      * fff
      */
}

3.1 BiPredicate

接受两个输入参数,返回true或false。

@Test
public void biPredicateTest() {
    BiPredicate<Integer, Integer> biPredicate = (a,b) -> a > b;
    System.out.println(biPredicate.test(1,2));
}

4. Supplier

无参数,返回一个结果。

@Test
public void supplierTest() {
    Supplier<Double> supplier = () -> {
        System.out.println("supplier");
        return Math.random();
    };
    System.out.println(supplier.get());
}

参考:
On Java 8 第十三章 函数式编程
Java8函数式编程
Java函数式编程详解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值