Java 内置四大核心函数式接口

  1. Lambda 表达式语法讲解
  2. Java 内置四大核心函数式接口
  3. 方法引用和构造器引用

Java 内置四大核心函数式接口

函数式接口参数类型返回类型用途
Consumer<T> 消费型接口Tvoid对类型为T的对象应用操作,无返回值,包含方法: void accept(T t);
Supplier<T> 供给型接口T返回类型为T的对象,包含方法:T get();
Function<T,R> 函数型接口TR对类型为T的对象应用操作,并返回结果。结果是R类型的对象。包含方 法:R apply(T t);
Predicate<T> 断定型接 口Tboolean确定类型为T的对象是否满足某约束,并返回 boolean 值。包含方法 boolean test(T t);

其他接口

函数式接口参数类型返回类型用途
BiFunction<T,U,R>T,UR对类型为 T, U 参数应用操作, 返回 R 类型的结果。包含方法为 Rapply(T t, U u);
UnaryOperator<T>(Function子接口)TT对类型为T的对象进行一元运算, 并返回T类型的结果。包含方法为 Tapply(T t);
BinaryOperator<T> (BiFunction 子接口)T,TT对类型为T的对象进行二元运算,并返回T类型的结果。包含方法为 Tapply(T t1, T t2);
BiConsumer<T,U>T,Uvoid对类型为T, U 参数应用操作。包含方法为 void accept(T t, U u);
ToIntFunction<T> ToLongFunction<T> ToDoubleFunction<T>Tint 、 long 、 double分 别 计 算 int 、 long 、 double、值的函数
IntFunction<R> LongFunction<R> DoubleFunction<R>int、long、 doubleR参数分别为int、long、 double 类型的函数

1. Consumer 消费型接口 示例

public class TestLambda {
	public static void main(String[] args) {
		TestLambda lambda = new TestLambda();
		lambda.happy(10000, (m) -> System.out.println("你们刚哥喜欢大宝剑,每次消费:" + m + "元"));;
	}

    // 消费
	public void happy(double money, Consumer<Double> con){
		con.accept(money);
	}
}

2. Supplier 供给型接口 示例

public class TestLambda {
	public static void main(String[] args) {
		TestLambda lambda = new TestLambda();
		List<Integer> numList = lambda.getNumList(10, () -> (int)(Math.random() * 100));

		for (Integer num : numList) {
			System.out.println(num);
		}
	}

	//需求:产生指定个数的整数,并放入集合中
	public List<Integer> getNumList(int num, Supplier<Integer> sup){
		List<Integer> list = new ArrayList<>();

		for (int i = 0; i < num; i++) {
			Integer n = sup.get();
			list.add(n);
		}

		return list;
	}
}

3. Function 函数型接口 示例

public class TestLambda {
	public static void main(String[] args) {
		TestLambda lambda = new TestLambda();
		String newStr = lambda.strHandler("\t\t\t Hello Lambda!   ", (str) -> str.trim());
		System.out.println(newStr);

		String subStr = lambda.strHandler("Hello Lambda!", (str) -> str.substring(2, 5));
		System.out.println(subStr);
	}

	//需求:用于处理字符串
	public String strHandler(String str, Function<String, String> fun){
		return fun.apply(str);
	}
}

4. Predicate 断言型接口 示例

public class TestLambda {
	public static void main(String[] args) {
		TestLambda lambda = new TestLambda();
		List<String> list = Arrays.asList("Hello", "wasu", "Lambda", "www", "ok");
		List<String> strList = lambda.filterStr(list, (s) -> s.length() > 3);

		for (String str : strList) {
			System.out.println(str);
		}
	}

	//需求:将满足条件的字符串,放入集合中
	public List<String> filterStr(List<String> list, Predicate<String> pre){
		List<String> strList = new ArrayList<>();
		for (String str : list) {
			if(pre.test(str)){
				strList.add(str);
			}
		}
		return strList;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值