基础Lambda表达式使用

package com.wsf.lambda.demo2;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleFunction;
import java.util.function.DoublePredicate;
import java.util.function.DoubleSupplier;
import java.util.function.DoubleToLongFunction;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author wsf
 * @since 20220224
 */
public class TestLambda {

    public static void main(String[] args) {
        // ----
        Function<String,String> function = x1 -> "hello:" + x1;
        System.out.println(function.apply("wsf")); //hello:wsf

        // ----
        DoubleToLongFunction doubleToLongFunction = TestLambda::testDoubleToLong;
        DoubleToLongFunction doubleToLongFunction2 = Math::round;
        System.out.println(doubleToLongFunction.applyAsLong(1.56));
        System.out.println(doubleToLongFunction2.applyAsLong(1.34));

        // ----
        DoubleSupplier doubleSupplier = () -> Math.round(ThreadLocalRandom.current().nextDouble(0, 999));
        System.out.println(doubleSupplier.getAsDouble());
        System.out.println(doubleSupplier.getAsDouble());
        System.out.println(doubleSupplier.getAsDouble());

        // ----
        DoublePredicate doublePredicate = value -> value > 500;
        DoublePredicate doublePredicate2 = value -> value > 700;
        // 取且
        boolean testAnd = doublePredicate.and(doublePredicate2).test(600);
        // 取或
        boolean testOr = doublePredicate.or(doublePredicate2).test(600);
        // 取反
        boolean testNegate = doublePredicate.negate().test(600);
        System.out.println("DoublePredicate and:  " + testAnd);
        System.out.println("DoublePredicate or:  " + testOr);
        System.out.println("DoublePredicate negate  :" + testNegate);

        // ----
        DoubleFunction<Double> doubleFunction = doubleValue -> doubleValue * 2;
        System.out.println(doubleFunction.apply(1.5));

        // ----
        DoubleConsumer doubleConsumer = doubleValue -> System.out.println(doubleValue * 2);
        doubleConsumer.accept(1.6);
        DoubleConsumer doubleConsumer2 = doubleValue -> System.out.println(doubleValue * 3);
        doubleConsumer.andThen(doubleConsumer2).accept(1.7);

        // ----
        DoubleBinaryOperator operator = (x1,x2) -> x1 * x2;
        System.out.println("DoubleBinaryOperator :  " + operator.applyAsDouble(1.5,1.5));

        // ----
        StringBuilder sb = new StringBuilder("Hello ");
        Consumer<StringBuilder> consumer = (str) -> str.append("Jack!");
        Consumer<StringBuilder> consumer1 = (str) -> str.append(" Bob!");
        consumer.andThen(consumer1).accept(sb);
        System.out.println(sb);	// Hello Jack! Bob!


        StringBuilder stringBuilder = new StringBuilder();
        BiConsumer<String,String > biConsumer = (x1,x2) -> {
            stringBuilder.append(x1);
            stringBuilder.append(x2);
        };
        biConsumer.accept("zhangsan%","%lisi");
        System.out.println(stringBuilder);//zhangsan%%lisi

        // ----

        List<Integer> list = Lists.newArrayList(1,5,8,7);
        System.out.println(list); // [1, 5, 8, 7]
        // 默认升序
        List<Integer> sortedList = list.stream().sorted().collect(Collectors.toList());
        System.out.println(sortedList); // [1, 5, 7, 8]
        // 降序排列
        System.out.println(list.stream().sorted((o1, o2) -> {
            if (o1 > o2) {
                return -1;
            } else {
                return 1;
            }
        }).collect(Collectors.toList()));
        
    }

    public static long testDoubleToLong(Double d){
        return Math.round(d * 2);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值