####一、方法引用分类
1.构造函数
2.静态方法
3.实例方法(类型)
4.实例方法(对象)
####二、方法引用-构造函数 ClassName::new,示例:
/**
* @author Kevin
* @date 2017-01-24
*/
public class ConstructorTest {
public static void main(String[] args) {
Function<String, String> function = 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) {
Function<Integer, String> function = 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) {
Function<String, Integer> function = 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");
Function<String, String> converter = 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) {
List<Company> companyList = 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<Company>() {
@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<Company> companySetLambda =
transferElements(companyList, () -> {
return new HashSet<>();
});
System.out.println("------companySetLambda------");
companySetLambda.forEach(System.out::println);
// 使用new运算符构造函数
Set<Company> companySet = transferElements(
companyList, HashSet::new);
System.out.println("------companySet------");
companySet.forEach(System.out::println);
}
/**
* 将集合中的元素复制到另一个集合
*
* @author Kevin
*/
public static <T, SOURCE extends Collection<T>, DEST extends Collection<T>>
DEST transferElements(
SOURCE sourceCollection,
Supplier<DEST> 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.Method References (http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html)