本文共 3989 字,大约阅读时间需要 13 分钟。
参考:
https://www.cnblogs.com/figure9/p/java-8-lambdas-insideout-language-features.html
https://blog.csdn.net/ioriogami/article/details/12782141Jdk8 就提供了 FunctionalInterface 的注解来帮助我们。
Java8中对FunctionalInterface注解的声明如下:@Documented@Retention(value=RUNTIME)@Target(value=TYPE)public @interface FunctionalInterface
其中,@Target(value=TYPE)表明@FunctionalInterface注解可用于类、接口(包括注释类型),或枚举声明 。
注:@Target中value的可取如下值:public static final ElementType TYPE
类、接口(包括注释类型),或枚举声明public static final ElementType FIELD
现场申报(包括枚举常量)public static final ElementType METHOD
方法的声明public static final ElementType PARAMETER
形式参数声明public static final ElementType CONSTRUCTOR
构造函数的声明public static final ElementType LOCAL_VARIABLE
局部变量声明public static final ElementType ANNOTATION_TYPE
注释类型声明public static final ElementType PACKAGE
包装声明public static final ElementType TYPE_PARAMETER
类型参数声明 从以下版本开始: 一点八public static final ElementType TYPE_USE
一种类型的使用 从以下版本开始: 一点八接下来我们就一起实现Java8的自定义函数编程。
1、创建自定义函数接口MyFunction
package com.lyz.java8.customer.function; /** * 自定义函数MyFunction,提供handler接口, 传入的是T,返回的是R * @author liuyazhuang * * @param* @param */@FunctionalInterfacepublic interface MyFunction { /** * 传入T,返回R * @param t1 * @param t2 * @return R */ R handler(T t1, T t2); }
2、创建Person类
package com.lyz.java8.customer.function.entity; /** * 自定义的Person类 * @author liuyazhuang * */public class Person { private String name; private Integer age; public Person(String name, Integer age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
3、创建测试自定义函数编程类FunctionTest
由于FunctionTest类中的注释已经很详细了,这里我就不再对每个方法一一列举了,大家直接看如下的代码,一看便知:
package com.lyz.java8.customer.function; import java.util.Arrays;import java.util.List;import java.util.stream.Collectors; import org.junit.Test; import com.lyz.java8.customer.function.entity.Person; /** * 测试自定义函数编程 * @author liuyazhuang * */public class FunctionTest { /** * 利用自定义函数编程计算x + y * @param x 第一个参数值x * @param y 第二个参数值y * @param myFunction 自定义的函数接口 * @return x + y */ public Long getAdd(Long x, Long y, MyFunctionmyFunction) { return myFunction.handler(x, y); } @Test public void testGetAdd() { Long result = getAdd(1L, 2L, (x, y) -> { return x + y; }); System.out.println("计算得出的结果为:" + result); } /** * 利用自定义函数编程将int类型转化为Spring类型 * @param x 第一个int数字 * @param y 第二个int数字 * @param myFunction 自定义的函数接口 * @return 将int转化为Spring的结果 */ public String getIntToString(int x, int y, MyFunction myFunction) { return myFunction.handler(x, y); } @Test public void testGetIntToString() { String result = getIntToString(18, 20, (x, y) -> { return "小明的年龄在".concat(String.valueOf(x)).concat("岁到").concat(String.valueOf(y)).concat("岁之间"); }); System.out.println(result); } /** * 利用自定义函数编程通过姓名和年龄过滤Person列表 * @param x 参数x * @param y 参数y * @param myFunction 自定义的函数接口 * @return 过滤后的Person列表 */ public List getPersonsByNameAndAge(String x, String y, MyFunction > myFunction){ return myFunction.handler(x, y); } @Test public void testGetPersonsByNameAndAge() { List list = Arrays.asList( new Person("刘亚壮", 20), new Person("张三", 26), new Person("李四", 27), new Person("小明", 18), new Person("小李", 30) ); String name = "小明"; String age = "18"; List persons = getPersonsByNameAndAge(name, age, (x, y) -> { List subPersons = list.stream().filter(p -> p.getAge() >= Integer.parseInt(y) && p.getName().equals(x)).collect(Collectors.toList()); return subPersons; }); persons.stream().forEach(System.out::println); }}