####一、方法引用分类
11.构造函数 22.静态方法 33.实例方法(类型) 44.实例方法(对象)
####二、方法引用-构造函数 ClassName::new,示例:
1/** 2 * @author Kevin 3 * @date 2017-01-24 4 */ 5public class ConstructorTest { 6 7 public static void main(String[] args) { 8 Function<String, String> function = String::new; 9 System.out.println(function.apply("SpaceX")); 10 } 11 12}
####三、方法引用-静态方法 ClassName::staticMethodName,示例:
1/** 2 * @author Kevin 3 * @date 2017-01-24 4 */ 5public class StaticMethodTest { 6 7 public static void main(String[] args) { 8 Function<Integer, String> function = String::valueOf; 9 System.out.println(function.apply(1001)); 10 } 11 12}
####三、方法引用-实例方法(类型) ClassName::instanceMethodName,示例:
1/** 2 * @author Kevin 3 * @date 2017-01-24 4 */ 5public class ClassNameInstanceMethodTest { 6 7 public static void main(String[] args) { 8 Function<String, Integer> function = String::length; 9 System.out.println(function.apply("Google")); 10 } 11 12}
####四、方法引用-实例方法(对象) object::instanceMethodName,示例:
1/** 2 * 公司 3 * 4 * @author Kevin 5 * @date 2017-01-24 6 */ 7public class Company { 8 9 /** 10 * 编码 11 */ 12 private String code; 13 14 /** 15 * 公司名称 16 */ 17 private String name; 18 19 public Company() { 20 21 } 22 23 public Company(String name, String code) { 24 this.name = name; 25 this.code = code; 26 } 27 28 public static int compareByCode(Company a, Company b) { 29 return a.getCode().compareTo(b.getCode()); 30 } 31 32 @Override 33 public String toString() { 34 return "Company{" + 35 "code='" + code + '\'' + 36 ", name='" + name + '\'' + 37 '}'; 38 } 39 40 /** 41 * 生产 42 * 43 * @author Kevin 44 */ 45 String produce(String productName) { 46 return getName() + " produces " + productName; 47 } 48 49 public String getCode() { 50 return code; 51 } 52 53 public void setCode(String code) { 54 this.code = code; 55 } 56 57 public String getName() { 58 return name; 59 } 60 61 public void setName(String name) { 62 this.name = name; 63 } 64 65} 66 67/** 68 * @author Kevin 69 * @date 2017-01-24 70 */ 71public class ObjectInstanceMethodTest { 72 73 public static void main(String[] args) { 74 Company company = new Company(); 75 company.setName("Apple"); 76 Function<String, String> converter = company::produce; 77 System.out.println(converter.apply("iPhone 7")); 78 } 79 80}
五、方法引用的综合使用
1/** 2 * Java8 方法引用 3 * 4 * @author Kevin 5 * @date 2017-01-24 6 */ 7public class MethodReferencesTest { 8 9 public static void main(String[] args) { 10 List<Company> companyList = new ArrayList<>(); 11 companyList.add(new Company("Google", "1002")); 12 companyList.add(new Company("SpaceX", "1001")); 13 companyList.add(new Company("Apple", "1003")); 14 15 System.out.println("------companyList------"); 16 companyList.forEach(System.out::println); 17 18 Company[] listAsArray = companyList.toArray(new Company[companyList.size()]); 19 20 // 不使用方法引用 21 Arrays.sort(listAsArray, new Comparator<Company>() { 22 23 @Override 24 public int compare(Company a, Company b) { 25 return a.getCode().compareTo(b.getCode()); 26 } 27 28 }); 29 30 // 使用lambada表达式 31 Arrays.sort(listAsArray, 32 (Company a, Company b) -> { 33 return a.getCode().compareTo(b.getCode()); 34 } 35 ); 36 37 // 方法引用(静态方法) 38 Arrays.sort(listAsArray, Company::compareByCode); 39 System.out.println("------listAsArray------"); 40 for (int i = 0; i < listAsArray.length; i++) { 41 System.out.println(listAsArray[i]); 42 } 43 44 class ComparisonProvider { 45 public int compareByName(Company a, 46 Company b) { 47 return a.getName().compareTo(b.getName()); 48 } 49 } 50 ComparisonProvider comparisonProvider = new ComparisonProvider(); 51 // 方法引用(实例方法) 52 Arrays.sort(listAsArray, comparisonProvider::compareByName); 53 54 // 使用lambada表达式构造函数 55 Set<Company> companySetLambda = 56 transferElements(companyList, () -> { 57 return new HashSet<>(); 58 }); 59 System.out.println("------companySetLambda------"); 60 companySetLambda.forEach(System.out::println); 61 62 // 使用new运算符构造函数 63 Set<Company> companySet = transferElements( 64 companyList, HashSet::new); 65 System.out.println("------companySet------"); 66 companySet.forEach(System.out::println); 67 68 } 69 70 /** 71 * 将集合中的元素复制到另一个集合 72 * 73 * @author Kevin 74 */ 75 public static <T, SOURCE extends Collection<T>, DEST extends Collection<T>> 76 DEST transferElements( 77 SOURCE sourceCollection, 78 Supplier<DEST> collectionFactory) { 79 80 DEST result = collectionFactory.get(); 81 for (T t : sourceCollection) { 82 result.add(t); 83 } 84 return result; 85 } 86 87} 88 89/** 90 * 公司 91 * 92 * @author Kevin 93 * @date 2017-01-24 94 */ 95public class Company { 96 97 /** 98 * 编码 99 */ 100 private String code; 101 102 /** 103 * 公司名称 104 */ 105 private String name; 106 107 public Company() { 108 109 } 110 111 public Company(String name, String code) { 112 this.name = name; 113 this.code = code; 114 } 115 116 public static int compareByCode(Company a, Company b) { 117 return a.getCode().compareTo(b.getCode()); 118 } 119 120 @Override 121 public String toString() { 122 return "Company{" + 123 "code='" + code + '\'' + 124 ", name='" + name + '\'' + 125 '}'; 126 } 127 128 /** 129 * 生产 130 * 131 * @author Kevin 132 */ 133 String produce(String productName) { 134 return getName() + " produces " + productName; 135 } 136 137 public String getCode() { 138 return code; 139 } 140 141 public void setCode(String code) { 142 this.code = code; 143 } 144 145 public String getName() { 146 return name; 147 } 148 149 public void setName(String name) { 150 this.name = name; 151 } 152 153}
六、参考文章
1.Method References (http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html)