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);
}
}
基础Lambda表达式使用
最新推荐文章于 2022-08-28 18:00:42 发布