Java8 MethodReference方法引用
来源:互联网 发布:托管淘宝 编辑:程序博客网 时间:2024/05/22 01:56
package com.lgx.jdk8.part02;import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.function.Function;import java.util.function.Supplier;/** * 方法引用的学习 */public class Test02MethodReference { public String getString(Supplier<String> supplier){ return supplier.get() + "test"; } public String getString2(String str, Function<String, String> function){ return function.apply(str); } public static void main(String[] args) { List<String> list = Arrays.asList("hello", "world", "hello world"); //之前学习Lambda的写法 list.forEach(item -> System.out.println("item = [" + item + "]")); //方法引用的写法 list.forEach(System.out::println); //方法引用共分为4类: //a、类名::静态方法名 //当你使用的Lambda表达式恰好有一个静态方法可以完成,则可以使用这种方式 Student student1 = new Student("zhangsan", 60); Student student2 = new Student("lisi", 80); Student student3 = new Student("wangwu", 50); Student student4 = new Student("zhaoliu", 78); List<Student> studentList = Arrays.asList(student1, student2, student3, student4); //studentList.sort((studentPara1, studentPara2) -> Student.compareStudentsByScore(studentPara1, studentPara2));//传统写法 /*studentList.sort(Student::compareStudentsByScore); studentList.forEach(student -> System.out.println("student = [" + student.getScore() + "]")); //b、引用名(对象名)::实例方法名 /*studentList.sort(new StudentComparator()::compareStudentsByScore); studentList.forEach(student -> System.out.println("student = [" + student.getScore() + "]"));*/ //c、类名::实例方法名 //与上面那个类似,Lambda表达式传递进来的第一个参数作为当前对象,当作调用者, //第二及以后的参数才传递给被调用的方法 /*studentList.sort(Student::compareByScore); studentList.forEach(student -> System.out.println("student = [" + student.getScore() + "]")); studentList.sort(Student::compareByName); studentList.forEach(student -> System.out.println("student = [" + student.getName() + "]"));*/ //拓展下 List<String> cites = Arrays.asList("jiangxi", "hubei", "shenzhen", "beijing"); //Collections.sort(cites, (city1, city2) -> city1.compareTo(city2));//传统写法 //cites.forEach(city -> System.out.println("city = [" + city + "]"));//传统写法 Collections.sort(cites, String::compareTo);//这里是第3种写法 cites.forEach(System.out::println);//这里是第2种写法 //d、类名::new //也叫构造方法引用 Test02MethodReference test = new Test02MethodReference(); String result = test.getString(String::new); System.out.println("result = [" + result + "]"); String result2 = test.getString2("hello", String::new); System.out.println("result2 = [" + result2 + "]"); }}class Student{ private String name; private int score; public Student(String name, int score) { this.name = name; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } //按照学生的成绩对学生进行排序 public static int compareStudentsByScore(Student student, Student student2){ return student.getScore() - student2.getScore(); } //按照学生的名字对学生进行排序 public static int compareStudentsByName(Student student, Student student2){ return student.getName().compareTo(student2.getName()); } //按照学生的成绩对学生进行排序 public int compareByScore(Student student){ return this.getScore() - student.getScore(); } //按照学生的名字对学生进行排序 public int compareByName(Student student){ return this.getName().compareTo(student.getName()); }}class StudentComparator{ //按照学生的成绩对学生进行排序 public int compareStudentsByScore(Student student, Student student2){ return student.getScore() - student2.getScore(); } //按照学生的名字对学生进行排序 public int compareStudentsByName(Student student, Student student2){ return student.getName().compareTo(student2.getName()); }}
阅读全文
0 0
- Java8 MethodReference方法引用
- Java8 的方法引用
- Java8方法引用(::)
- java8之方法引用
- Java8---5.方法引用
- Java8之方法引用
- java8 方法引用
- Java8 _ 方法引用 ::
- java8中的方法引用
- java8-静态方法引用
- java8之方法引用
- Java8之方法引用
- java8 - 方法引用(method referrance)
- java8 - 方法引用(method referrance)
- java8系列之方法引用
- Java8 实战学习 方法引用
- Java8 方法引用-Method References
- Java8新特性--方法引用
- Android----ViewPager页面滑动基础--PagerAdapter篇(一)
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构与算法2:字符串与数组
- git ssh key 生成
- minifilter 向应用层发送信息fltSendMessage
- Java8 MethodReference方法引用
- 编写一个手机类(Mobile),包括手机品牌(brand)、手机型号(type), 方法包括显示手机信息,并编写测试类进行对象的创建
- Hadoop入门之Hbase得安装和简单Shell使用
- Bash和Ksh中查找和执行历史命令
- jquery过滤器,过滤器它是在基本选择器与层次选择器获取到一批元素后,再进行过滤操作
- Spring MVC RequestParam RequestHeader CookieValue 注解
- json工具箱
- Java8 DefaultMethod
- 文章标题