函数式接口

  • 一. Java8 内置的四大核心函数式接口
  • ①. 消费型接口:Consumer
  • ②. 供给型接口:Supplier
  • ③. 函数型接口:Function
  • ④. 断言型接口:Predicate
  • ⑤. 其他接口
  • 二. 方法引用
  • ①. 类名 :: 实例方法名
  • ②. 类名 :: 静态方法名
  • ③. 对象的引用 :: 实例方法名
  • 三. 构造器引用
  • 四. 数组引用


一. Java8 内置的四大核心函数式接口

函数式接口:只有一个抽象方法的接口,可用@FunctionalInterface

  • 我们可以在任意函数式接口上使用 @FunctionalInterface 注解,这样做可以检查它是否是一个函数式接口,同时 javadoc 也会包含一条声明,说明这个接口是一个函数式接口。

java 范型 T 怎么拿到对应的 Class java范型接口_System

①. 消费型接口:Consumer

java 范型 T 怎么拿到对应的 Class java范型接口_Test_02

//Consumer<T> 消费型接口  传入T 无返回
	@Test
	public void test1(){
		happy(10000, (m) -> System.out.println("戏子zzzz,每次消费:" + m + "元"));
	} 
	
	public void happy(double money, Consumer<Double> con){
		con.accept(money);
	}
}

②. 供给型接口:Supplier

java 范型 T 怎么拿到对应的 Class java范型接口_System_03

//Supplier<T> 供给型接口  无传入 返回T
	@Test
	public void test2(){
		List<Integer> numList = getNumList(10, () -> (int)(Math.random() * 100));
			System.out.println(numList.toString());
	}
	//需求:产生指定个数的整数,并放入集合中
	public List<Integer> getNumList(int num, Supplier<Integer> sup){
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < num; i++) {
			list.add(sup.get());
		}
		return list;
	}

③. 函数型接口:Function

java 范型 T 怎么拿到对应的 Class java范型接口_函数式接口_04

//Function<T, R> 函数型接口: 传入T 返回R
	@Test
	public void test3(){
		String newStr = strHandler("\t\t\t 戏子zzz   ", (str) -> str.trim());
		System.out.println(newStr);
		
		String subStr = strHandler("戏子zzz", (str) -> str.substring(2, 5));
		System.out.println(subStr);
	}
	//需求:用于处理字符串
	public String strHandler(String str, Function<String, String> fun){
		return fun.apply(str);
	}

④. 断言型接口:Predicate

java 范型 T 怎么拿到对应的 Class java范型接口_函数式接口_05

//Predicate<T> 断言型接口 传入T 返回Boolean
	@Test
	public void test4(){
		List<String> list = Arrays.asList("Hello", "xizizzz", "Lambda", "www", "ok");
		List<String> strList = 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;
	}

⑤. 其他接口

java 范型 T 怎么拿到对应的 Class java范型接口_函数式接口_06

二. 方法引用

  • 方法引用理解为 Lambda 表达式的另外一种表现形式
  • 当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用! (实现抽象方法的参数列表,必须与方法引用方法的参数列表保持一致!)
  • 方法引用:使用操作符 "::"将方法名和对象或类的名字分隔开来。

①. 类名 :: 实例方法名

java 范型 T 怎么拿到对应的 Class java范型接口_函数式接口_07

//类名 :: 实例方法名
	@Test
	public void test5(){
		// BiPredicate 传入两个参数 返回一个布尔
		BiPredicate<String, String> bp = (x, y) -> x.equals(y);
		System.out.println(bp.test("abcde", "abcde"));
		System.out.println("-----------------------------------------");
		BiPredicate<String, String> bp2 = String::equals;
		System.out.println(bp2.test("abc", "abc"));
		
		System.out.println("-----------------------------------------");
		Function<Employee, String> fun = (e) -> e.show();
		System.out.println(fun.apply(new Employee()));
		System.out.println("-----------------------------------------");
		Function<Employee, String> fun2 = Employee::show;
		System.out.println(fun2.apply(new Employee()));
	}

②. 类名 :: 静态方法名

java 范型 T 怎么拿到对应的 Class java范型接口_Test_08

java 范型 T 怎么拿到对应的 Class java范型接口_函数式接口_09

//类名 :: 静态方法名
	@Test
	public void test4(){
		Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
		
		System.out.println("-------------------------------------");
		// Comparator 函数式接口的参数和返回值类型要和 静态compare方法一致
		Comparator<Integer> com2 = Integer::compare;
	}

③. 对象的引用 :: 实例方法名

java 范型 T 怎么拿到对应的 Class java范型接口_System_10

==

//对象的引用 :: 实例方法名
	@Test
	public void test2(){
		Employee emp = new Employee(101, "张三", 18, 9999.99);
		Supplier<String> sup = () -> emp.getName();
		System.out.println(sup.get());
		System.out.println("----------------------------------");
		Supplier<String> sup2 = emp::getName;
		System.out.println(sup2.get());
	}

java 范型 T 怎么拿到对应的 Class java范型接口_函数式接口_11

// 对象的引用 :: 实例方法名
	@Test
	public void test1(){
		PrintStream ps = System.out;
		Consumer<String> con = (str) -> ps.println(str);
		con.accept("Hello World!");
		System.out.println("--------------------------------");
		Consumer<String> con2 = ps::println;
		con2.accept("Hello Java8!");
		Consumer<String> con3 = System.out::println;
	}

三. 构造器引用

  • 与函数式接口相结合,自动与函数式接口中方法兼容。可以把构造器引用赋值给定义的方法,与构造器参数列表要与接口中抽象方法的参数列表一致!
//构造器引用
	@Test
	public void test7(){
		//Lambda形式
		Function<String,Employee> fun1=(x) -> new Employee(x);
		// 传入一个参数 ::形式
		Function<String, Employee> fun3 = Employee::new;
		// 传入两个参数
		BiFunction<String, Integer, Employee> fun4 = Employee::new;
	}

java 范型 T 怎么拿到对应的 Class java范型接口_函数式接口_12

@Test
	public void test6(){
		Supplier<Employee> sup1 = () -> new Employee();
		Supplier<Employee> sup2 = Employee::new;
		System.out.println(sup2.get());
	}

四. 数组引用

java 范型 T 怎么拿到对应的 Class java范型接口_System_13

//数组引用
	@Test
	public void test8(){
		//Lambda形式
		Function<Integer, String[]> fun1 = (args) -> new String[args];
		//:: 形式
		Function<Integer, String[]> fun2 = String[]::new;
		String[] strs = fun2.apply(10);
		System.out.println(strs.length);
		System.out.println("--------------------------");
		Function<Integer, Employee[]> fun3 = Employee[] :: new;
		Employee[] emps = fun3.apply(20);
		System.out.println(emps.length);
	}