Java8 方法引用的使用

####一、方法引用分类

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)

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java日期时间API系列31

  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前

java构造方法

1.构造方法(函数)就是用来构造对象的方法,分为有参和无参两个类型。例如下面例子a.HellohellonewHello(); b.HellohellonewHello(Stringname); a是一个无参的构造方法,b是传入一个参数是string的构造方法。两者都是用来实例化对象。实例化对象的格式可以是:Class

Java8特性

Java8又称jdk1.8。主要新特性:Lambda表达式 −Lambda允许把函数作为一个方法的参数(函数作为参数传递进方法中。方法引用 −方法引用提供了非常有用的语法,可以直接引用已有Java类或对象(实例)的方法或构造器。与lambda联合使用,方法引用可以使语言的构造更紧凑简洁,减少冗余代码。