Java之——Java8 自定义函数编程

转载请注明出处:https://blog.csdn.net/l1028386804/article/details/80390682

一、FunctionalInterface描述

Jdk8 就提供了 FunctionalInterface 的注解来帮助我们。
Java8中对FunctionalInterface注解的声明如下:

@Documented
@Retention(value=RUNTIME)
@Target(value=TYPE)
public @interface FunctionalInterface
其中,@Target(value=TYPE)表明@FunctionalInterface注解可用于类、接口(包括注释类型),或枚举声明 。
注:@Target中value的可取如下值:
  • TYPE
public static final ElementType TYPE
类、接口(包括注释类型),或枚举声明
  • FIELD
public static final ElementType FIELD
现场申报(包括枚举常量)
  • METHOD
public static final ElementType METHOD
方法的声明
  • PARAMETER
public static final ElementType PARAMETER
形式参数声明
  • CONSTRUCTOR
public static final ElementType CONSTRUCTOR
构造函数的声明
  • LOCAL_VARIABLE
public static final ElementType LOCAL_VARIABLE
局部变量声明
  • ANNOTATION_TYPE
public static final ElementType ANNOTATION_TYPE
注释类型声明
  • PACKAGE
public static final ElementType PACKAGE
包装声明
  • TYPE_PARAMETER
public static final ElementType TYPE_PARAMETER
类型参数声明
从以下版本开始:
一点八
  • TYPE_USE
public static final ElementType TYPE_USE
一种类型的使用
从以下版本开始:
一点八

接下来我们就一起实现Java8的自定义函数编程。

二、具体实现

1、创建自定义函数接口MyFunction
package com.lyz.java8.customer.function;

/**
 * 自定义函数MyFunction,提供handler接口, 传入的是T,返回的是R
 * @author liuyazhuang
 *
 * @param <T>
 * @param <R>
 */
@FunctionalInterface
public interface MyFunction<T, R> {
	
	/**
	 * 传入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, MyFunction<Long, Long> myFunction) {
		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<Integer, String> 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<Person> getPersonsByNameAndAge(String x, String y, MyFunction<String, List<Person>> myFunction){
		return myFunction.handler(x, y);
	}
	
	@Test
	public void testGetPersonsByNameAndAge() {
		List<Person> list = Arrays.asList(
				new Person("刘亚壮", 20),
				new Person("张三", 26),
				new Person("李四", 27),
				new Person("小明", 18),
				new Person("小李", 30)
		);
		
		String name = "小明";
		String age = "18";
		List<Person> persons = getPersonsByNameAndAge(name, age, (x, y) -> {
			List<Person> 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);
	}
}


  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰 河

可以吃鸡腿么?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值