JDK8提供的基础函数接口Supplier,Function,BiFunction,BinaryOperator,Consumer,Predicate,Optional的使用方法

Supplier<T>为函数式接口 不接受参数返回一个值

public class SupplierTest {

	public static void main(String[] args) {
		
		SupplierTest test = new SupplierTest();
		
		Person person = new Person("zhangshan","1");
		
		//以下等价
		//1.
		Supplier<Person> supplier = ()->{
			return person;
		};
		Person person1 = test.getPerson(supplier);
		
		//2.
		Person person2 = test.getPerson(()->person);

	}
	
	//Supplier<T>为函数式接口 不接受参数返回一个值
	public Person getPerson(Supplier<Person> supplier) {
		return supplier.get();
	}
}

Function为函数式接口 输入一个参数返回一个参数

public class FunctionTest {

	public static void main(String[] args) {
		FunctionTest function = new FunctionTest();
		
		// 2 * 2
		System.out.println(function.compute(2,value -> value*2)); //4
		
		// (2 ^ 2) * 3
		System.out.println(function.compute2(2,value -> value*3, value -> value*value)); //12
		
		// (2 * 3) ^ 2
		System.out.println(function.compute3(2,value -> value*3, value -> value*value)); //36
		
		// 1 * 2
		System.out.println(function.compute4(1, 2, (v1,v2) -> v1 * v2)); //2
		
		// (1 * 2) * 2
		System.out.println(function.compute5(1, 2, (v1,v2) -> v1 * v2, v -> v*2)); //4
		
		// 1 * 2
		System.out.println(function.compute6(1, 2, (v1,v2) -> v1 * v2)); //2
	}
	
	//Function为函数式接口 输入一个参数返回一个参数
	//Function<T, R> T为入参类型 R为出参类型  
	public int compute(int a, Function<Integer, Integer> function) {
		return function.apply(a);
	}

	//函数组合
	public int compute2(int a, Function<Integer, Integer> f1, Function<Integer, Integer> f2) {
		//f2 -> f1 f2执行后执行f1 f2执行的结果为f1的入参
		return f1.compose(f2).apply(a);
	}
	
	public int compute3(int a, Function<Integer, Integer> function, Function<Integer, Integer> function2) {
		//f1 -> f2 f1执行后执行f2 f1执行的结果为f2的入参
		return function.andThen(function2).apply(a);
	}
	
	//BiFunction为函数式接口 输入两个参数返回一个参数
	//BiFunction<T, U, R> T为第一个参数类型 U为第二个参数类型 R为返回值的类型
	public int compute4(int a,int b,BiFunction<Integer,Integer,Integer> function) {
		return function.apply(a, b);
	}
	
	//BiFunction与Function组合使用
	public int compute5(int a,int b,BiFunction<Integer,Integer,Integer> biFunction,Function<Integer,Integer> function) {
		//biFunction -> function 执行biFunction后返回的结果作为function的入参
		return biFunction.andThen(function).apply(a, b);
	}
	
	//BinaryOperator为BiFunction的特殊情况 继承与BiFunction<T,T,T>
	public int compute6(Integer a,Integer b, BinaryOperator<Integer> binaryOperator) {
		return binaryOperator.apply(a, b);
	}
}

Consumer<T>为函数式接口 接收一个参数不返回值

public class ConsumerTest {

	public static void main(String[] args) {
		
		ConsumerTest test = new ConsumerTest();
		
		Person p1 = new Person("zhangsan","男");
		
		//将输入的person的sex属性改为女
		test.consume(p1, i -> i.setSex("女"));
		
		System.out.println(p1); //zhangsan 女

	}
	
	//Consumer<T>为函数式接口 接收一个参数不返回值
	public void consume(Person i, Consumer<Person> consumer) {
		consumer.accept(i);
	}
}

Predicate<T> 断言 传入值返回一个boolean值 通常传入一个判断条件

public class PredicateTest {

	public static void main(String[] args) {
		
		PredicateTest test = new PredicateTest();
		
		List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9);
		
		//传入条件为 值小于5为真
		test.getInt(list, value-> value < 5);
		
	}
	
	//Predicate<T> 断言 传入值返回一个boolean值 通常传入一个判断条件
	public void getInt(List<Integer> list,Predicate<Integer> predicate) {
		//遍历数组将符合predicate的值打印出来
		list.forEach(i -> {
			if(predicate.test(i)) {
				System.out.println(i); // 1 2 3 4
			}
		});
		
	}

}

java8 引入Optional<T>去解决空指针异常的问题

public class OptionalTest {

	public static void main(String[] args) {

		//java8 引入Optional<T>去解决空指针异常的问题
		//1.Optional的声明 创建一个Optional包装的字符串类型数据
		Optional<String> optional = Optional.of("hello world");
		//optional.isPresent()判断optional是否存在 再通过optional.get()获取到optional内的对象
		//但这种方式是不推荐的 是面向对象编程的思想
		if(optional.isPresent()) System.out.println(optional.get()); //hello world
		//推荐使用函数式编程
		optional.ifPresent(value -> System.out.println(value)); //hello world

		Company company = new Company();
		
		Person p1 = new Person("zhangsan","1");
		Person p2 = new Person("lishi","1");
		
		company.setName("Apple");
		company.setPersonList(Arrays.asList(p1,p2));
		
		Optional<Company> companyOptional = Optional.ofNullable(company);
		
		companyOptional.ifPresent(com -> com.getPersonList().stream().forEach(System.out::println)); //zhangsan 1  lishi 1
		
		company.setPersonList(null);
		
		//map映射 接收Founction函数 接收一个参数返回一个值
		//orElse方法当为空时执行
		List<Person> personList = companyOptional.map(com -> com.getPersonList()).orElse(Collections.emptyList());
		
		String name = companyOptional.map(com -> com.getName()).orElse("");
		
		System.out.println(personList);
		
		System.out.println(name);
		
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值