栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java8新特性

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java8新特性

 

package com.zs.boot.controller;

import java.io.PrintStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import org.junit.Test;


public class TestMethodRef {

	public class Employee{
		private String name;
		private int age;
		private double salary;

		public Employee() {
		}

		public Employee(int age) {
			this.age = age;
		}

		public Employee(String name, int age) {
			this.name = name;
			this.age = age;
		}

		public Employee(String name, int age, double salary) {
			this.name = name;
			this.age = age;
			this.salary = salary;
		}

		public String show() {
			return "测试方法引用!";
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public double getSalary() {
			return salary;
		}

		public void setSalary(double salary) {
			this.salary = salary;
		}

		@Override
		public String toString() {
			return "Employee{" +
					"name='" + name + ''' +
					", age=" + age +
					", salary=" + salary +
					'}';
		}
	}

	List employees = Arrays.asList(
			new Employee("刘德华",48,8000),
			new Employee("张学友",28,8500),
			new Employee("黎明",45,6500),
			new Employee("郭富城",51,5500));

	@Test
	public void test1(){
		Consumer con = (str) -> System.out.println(str);
		con.accept("你好!");
		

		PrintStream ps = System.out;
		Consumer con1 = ps::println;
		con1.accept("大家好");

		//最终版,对象的引用 :: 实例方法名
		Consumer con2 = System.out::println;
		con2.accept("Hello Java8!");
	}

	//--------------------------------------------------------------------

	//对象的引用 :: 实例方法名
	@Test
	public void test2(){
		Employee emp = new Employee("周华健", 55, 9999);

		Supplier sup = () -> emp.getName();
		String str = sup.get();
		System.out.println(str);

		System.out.println("----------------------------------");

		Supplier sup2 = emp::getName;
		System.out.println(sup2.get());
	}

	//--------------------------------------------------------------------
	//类名 :: 静态方法名
	@Test
	public void test3(){
		Comparator com = (x, y) -> Integer.compare(x, y);
		int compare1 = com.compare(12,21); //-1
		System.out.println(compare1);
		

		System.out.println("-------------------------------------");

		Comparator com2 = Integer::compare;
		int compare2 = com.compare(12,21); //-1
		System.out.println(compare2);
	}


	//--------------------------------------------------------------------
	//类名 :: 实例方法名
	@Test
	public void test4(){
		BiPredicate bp = (x, y) -> x.equals(y);
		System.out.println(bp.test("abcde", "abcde"));

		//若Lambda的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: ClassName::MethodName
		//(x, y) -> x.equals(y)
		BiPredicate bp2 = String::equals;
		System.out.println(bp2.test("abc", "abc"));

		System.out.println("-----------------------------------------");


		Function fun = (e) -> e.show();
		System.out.println(fun.apply(new Employee()));

		Function fun2 = Employee::show;
		System.out.println(fun2.apply(new Employee()));

	}

	@Test
	public void test5(){
		BiFunction fun = (x, y) -> Math.max(x, y);
		System.out.println(fun.apply(1.5, 22.2));

		System.out.println("--------------------------------------------------");

		BiFunction fun2 = Math::max;
		System.out.println(fun2.apply(1.2, 1.5));
	}

	//--------------------------------------------------------------------
	//构造器引用 :构造器的参数列表,需要与函数式接口中参数列表保持一致!
	@Test
	public void test6(){
		Supplier sup = () -> new Employee();
		System.out.println(sup.get());//Employee{name='null', age=0, salary=0.0}

		System.out.println("------------------------------------");

		Supplier sup2 = Employee::new; //匹配无参构造器
		Employee emp = sup2.get();//返回一个对象
		System.out.println(emp);//Employee{name='null', age=0, salary=0.0}
	}

	@Test
	public void test7(){
		Function fun = (x) -> new Employee();//匹配一个参数的构造器

		Function fun2 = Employee :: new;//匹配一个参数的构造器
		Employee emp = fun2.apply(1000);
		System.out.println(emp);

		//传两个参数,必须要有两个参数的构造器
		BiFunction fun3 = Employee::new;//匹配两个参数的构造器
	}

	//--------------------------------------------------------------------

	//数组引用
	@Test
	public void test8(){
		Function fun = (args) -> new String[args];
		String[] strs = fun.apply(10);
		System.out.println(strs.length);//数组长度为10
		
		System.out.println("--------------------------");
		
		Function fun2 = Employee[] :: new;
		Employee[] emps = fun2.apply(20);
		System.out.println(emps.length);//数组长度为20
	}
	

	
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/445428.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号