1 Function.apply 函数定义
@Test
public void biFunction() {
Function<String, String> function = (name) -> {
log.info("BiFunction 函数调用,一个参数,一个返回值");
return "我是" + name;
};
String ret = function.apply("张三");
log.info("ret:{}", ret);
//优化版,两个参数一个返回值
BiFunction<String, Integer, String> biFunction = (name, age) -> {
log.info("BiFunction 函数调用,两个参数,一个返回值");
return "我是" + name + "年龄:" + age;
};
ret = biFunction.apply("张三", 100);
log.info("ret:{}", ret);
//比较常用的方式【函数调用者自定义函数】
divApplyFunction("张三",(name)->{
log.info("函数调用者自定义函数");
return "我是"+name;
});
}
private void divApplyFunction(String name,Function<String, ?> function){
log.info("返回结果:{}",function.apply(name));
}
2 Predicate.test 条件判断
/**
* 条件判断
*/
@Test
public void predicate() {
Predicate<String> filter = (name) -> {
if (name.equals("张三")) {
return true;
} else {
return false;
}
};
if (filter.test("张三")) {
log.info("单参数,校验通过");
} else {
log.info("单参数,校验不通过");
}
//双参数
BiPredicate<String, Integer> filter2 = (name, age) -> {
if (name.equals("张三") && age == 30) {
return true;
} else {
return false;
}
};
if (filter2.test("张三", 30)) {
log.info("双参数,校验通过");
} else {
log.info("双参数,校验不通过");
}
//比较常用的方式【函数调用者自定义函数】
divBiPredicate("张三", 30, (name, age) -> {
if (name.equals("张三") && age == 30) {
return true;
} else {
return false;
}
});
}
private void divBiPredicate(String name, Integer age, BiPredicate<String, Integer> biPredicate) {
if (biPredicate.test(name, age)) {
log.info("校验通过,{},{}",name,age);
divBiPredicate("李四", 25,biPredicate);
}else {
log.info("校验不通过,{},{}",name,age);
}
}
3 Consumer.accept 函数定义
/**
* 先定义函数内容,后进行调用
*/
@Test
public void consumer() {
log.info("一个参数");
Consumer<Student> consumer = (student) -> {
student.setAge(30);
student.setName("张三");
};
Student student = Student.builder().build();
consumer.accept(student);
log.info("student:{}", student);
log.info("两个参数");
BiConsumer<Student, String> consumer2 = (student2, s) -> {
student2.setAge(30);
student2.setName(s + "张三");
};
Student student2 = Student.builder().build();
consumer2.accept(student2, "福建");
log.info("student2:{}", student2);
//比较常用的方式【函数调用者自定义函数】
divConsumer(Student.builder().age(20).build(), (divStudent) -> {
divStudent.setName("王五");
});
}
private void divConsumer(Student student, Consumer<Student> consumer) {
consumer.accept(student);
log.info("student:{}", student);
}
4 Supplier.get 函数定义
/**
* 读取定义对象
*/
@Test
public void supplier() {
Supplier<Student> supplier = () -> {
log.info("定义对象");
return Student.builder().age(30).name("张三").build();
};
Student student = supplier.get();
log.info("student:{}", student);
int age=30;
//比较常用的方式【函数调用者自定义函数】
divSupplier("阿三",()-> Student.builder().age(age).build());
}
private void divSupplier(String name,Supplier<Student> supplier){
Student student=supplier.get();
student.setName(name);
log.info("结果:"+student);
}
5 Comparator比较器
//比较器
@Test
public void comparator() {
Comparator<Integer> comparator = (num1, num2) -> {
if (num1 == num2) {
return 0;
} else if (num1 > num2) {
return 1;
} else {
return -1;
}
};
log.info("ret:" + comparator.compare(100, 90));
log.info("ret:" + comparator.reversed().compare(100, 90));
log.info("ret:" + comparator.compare(100, 100));
}
//比较器
@Test
public void binaryOperator() {
Comparator<Integer> comparator = (num1, num2) -> {
if (num1.equals(num2)) {
return 0;
} else if (num1 > num2) {
return 1;
} else {
return -1;
}
};
int apply = BinaryOperator.maxBy(comparator).apply(100, 90);
log.info("获取更大的:" + apply);
apply = BinaryOperator.minBy(comparator).apply(100, 90);
log.info("获取更小的:" + apply);
}
6 @FunctionalInterface自定义函数接口
@Test
public void test1() {
Students students = new Students();
String ret = replacePlaceholders("张三", 20, students::getName);//定义需要传的参数值和函数名称
log.info("函数式编程返回值:{}",ret);
}
public String replacePlaceholders(String name, Integer age, MyFunctionalInterface.PlaceholderResolver placeholderResolver) {
name=name+"8080【调用函数里面修改】";
return placeholderResolver.resolvePlaceholder(name, age);
}
//被调用的类和方法
class Students {
public String getName(String name, Integer age) {
return "姓名:" + name + ",年龄:" + age ;
}
}
//定义与被调用类绑定接口和接口函数
@FunctionalInterface
public interface PlaceholderResolver {
@Nullable
String resolvePlaceholder(String name, Integer age);
}
7 Optional非空校验
/**
* 对属性判断
*/
@Test
public void test1_base() {
//简单字符串的判空
String name = "zhangsan";
name = Optional.ofNullable(name).orElse("unKnow");
log.info("nam:{}", name);
name = null;
name = Optional.ofNullable(name).orElse("orElse->unKnow");
log.info("nam:{}", name);
name = null;
name = Optional.ofNullable(name).orElseGet(()-> "orElseGet->unKnow");
log.info("nam:{}", name);
}
/**
* 对象查询固定属性值
*/
@Test
public void test2_object_map(){
String name;
//对象判断
Student student1 = new Student(123, "zhangsan", 30);
Optional<Integer> optional = Optional.ofNullable(student1).flatMap((e) -> Optional.of(e.getAge()));
log.info("name:" + optional.get());
name = Optional.ofNullable(student1).map((v) -> v.getName()).get();
log.info("name:" + name);
Student student2 = new Student(111, null, 30);
//对象的指定属性为空返回固定值
String name2 = Optional.ofNullable(student2).map(Student::getName).orElse("unKnow");
log.info("name2:" + name2);
}
/**
* 空对象判断
*/
@Test
public void test3_object_null() throws Throwable{
Student student =null;
//当对象为空直接返回新的对象
Student student2 = Optional.ofNullable(student).orElseGet(() -> {
Student student3 = new Student(111, "lisi", 30);
return student3;
});
log.info("student2:" + student2);
//当对象为空直接抛异常,不为空直接返回对象
Student student4 = Optional.ofNullable(student2).orElseThrow(() -> new Throwable("null"));
log.info("student4:"+student4);
}
/**
* 非空对象查询
*/
@Test
public void test4_object_null(){
Student student1 = new Student(123, "wangwu", 30);
//对象非空直接处理业务
Optional.ofNullable(student1).ifPresent(u->doSomthing(student1));
}
private void doSomthing(Student student){
log.info("student:"+student);
}
|