####一、方法引用分类
1.构造函数2.静态方法3.实例方法(类型)4.实例方法(对象)
####二、方法引用-构造函数 ClassName::new,示例:
/** * @author Kevin * @date 2017-01-24 */public class ConstructorTest { public static void main(String[] args) { Functionfunction = String::new; System.out.println(function.apply("SpaceX")); }}
####三、方法引用-静态方法 ClassName::staticMethodName,示例:
/** * @author Kevin * @date 2017-01-24 */public class StaticMethodTest { public static void main(String[] args) { Functionfunction = String::valueOf; System.out.println(function.apply(1001)); }}
####三、方法引用-实例方法(类型) ClassName::instanceMethodName,示例:
/** * @author Kevin * @date 2017-01-24 */public class ClassNameInstanceMethodTest { public static void main(String[] args) { Functionfunction = String::length; System.out.println(function.apply("Google")); }}
####四、方法引用-实例方法(对象) object::instanceMethodName,示例:
/** * 公司 * * @author Kevin * @date 2017-01-24 */public class Company { /** * 编码 */ private String code; /** * 公司名称 */ private String name; public Company() { } public Company(String name, String code) { this.name = name; this.code = code; } public static int compareByCode(Company a, Company b) { return a.getCode().compareTo(b.getCode()); } @Override public String toString() { return "Company{" + "code='" + code + '\'' + ", name='" + name + '\'' + '}'; } /** * 生产 * * @author Kevin */ String produce(String productName) { return getName() + " produces " + productName; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; }}/** * @author Kevin * @date 2017-01-24 */public class ObjectInstanceMethodTest { public static void main(String[] args) { Company company = new Company(); company.setName("Apple"); Functionconverter = company::produce; System.out.println(converter.apply("iPhone 7")); }}
五、方法引用的综合使用
/** * Java8 方法引用 * * @author Kevin * @date 2017-01-24 */public class MethodReferencesTest { public static void main(String[] args) { ListcompanyList = new ArrayList<>(); companyList.add(new Company("Google", "1002")); companyList.add(new Company("SpaceX", "1001")); companyList.add(new Company("Apple", "1003")); System.out.println("------companyList------"); companyList.forEach(System.out::println); Company[] listAsArray = companyList.toArray(new Company[companyList.size()]); // 不使用方法引用 Arrays.sort(listAsArray, new Comparator () { @Override public int compare(Company a, Company b) { return a.getCode().compareTo(b.getCode()); } }); // 使用lambada表达式 Arrays.sort(listAsArray, (Company a, Company b) -> { return a.getCode().compareTo(b.getCode()); } ); // 方法引用(静态方法) Arrays.sort(listAsArray, Company::compareByCode); System.out.println("------listAsArray------"); for (int i = 0; i < listAsArray.length; i++) { System.out.println(listAsArray[i]); } class ComparisonProvider { public int compareByName(Company a, Company b) { return a.getName().compareTo(b.getName()); } } ComparisonProvider comparisonProvider = new ComparisonProvider(); // 方法引用(实例方法) Arrays.sort(listAsArray, comparisonProvider::compareByName); // 使用lambada表达式构造函数 Set companySetLambda = transferElements(companyList, () -> { return new HashSet<>(); }); System.out.println("------companySetLambda------"); companySetLambda.forEach(System.out::println); // 使用new运算符构造函数 Set companySet = transferElements( companyList, HashSet::new); System.out.println("------companySet------"); companySet.forEach(System.out::println); } /** * 将集合中的元素复制到另一个集合 * * @author Kevin */ public static , DEST extends Collection > DEST transferElements( SOURCE sourceCollection, Supplier collectionFactory) { DEST result = collectionFactory.get(); for (T t : sourceCollection) { result.add(t); } return result; }}/** * 公司 * * @author Kevin * @date 2017-01-24 */public class Company { /** * 编码 */ private String code; /** * 公司名称 */ private String name; public Company() { } public Company(String name, String code) { this.name = name; this.code = code; } public static int compareByCode(Company a, Company b) { return a.getCode().compareTo(b.getCode()); } @Override public String toString() { return "Company{" + "code='" + code + '\'' + ", name='" + name + '\'' + '}'; } /** * 生产 * * @author Kevin */ String produce(String productName) { return getName() + " produces " + productName; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
六、参考文章
1.