method reference
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier; public class MethodReferenceTest { 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) { MethodReferenceTest methodReferenceTest = new MethodReferenceTest(); System.out.println(methodReferenceTest.getString(String::new));
System.out.println(methodReferenceTest.getString2("hello", String::new)); Student student1 = new Student("zhangsan", 10);
Student student2 = new Student("lisi", 90);
Student student3 = new Student("wangwu", 50);
Student student4 = new Student("zhaoliu", 40); List<Student> students = Arrays.asList(student1, student2, student3, student4); // students.sort((studentParam1, studentParam2) ->
// Student.compareStudentByScore(studentParam1, studentParam2));
// students.forEach(student -> System.out.println(student.getScore()));
//
// System.out.println("-------"); // students.sort(Student::compareStudentByScore);
// students.forEach(student -> System.out.println(student.getScore()));
//
// System.out.println("-------");
//
// students.sort((studentParam1, studentParam2) ->
// Student.compareStudentByName(studentParam1, studentParam2));
// students.forEach(student -> System.out.println(student.getName()));
//
// System.out.println("-------");
//
// students.sort(Student::compareStudentByName);
// students.forEach(student -> System.out.println(student.getName()));
//
// System.out.println("-------");
//
// StudentComparator studentComparator = new StudentComparator();
//
// students.sort((studentParam1, studentParam2) ->
// studentComparator.compareStudentByScore(studentParam1, studentParam2));
// students.forEach(student -> System.out.println(student.getScore())); // System.out.println("-------");
//
// students.sort(studentComparator::compareStudentByScore);
// students.forEach(student -> System.out.println(student.getScore()));
//
// System.out.println("-------");
//
// students.sort((studentParam1, studentParam2) ->
// studentComparator.compareStudentByName(studentParam1, studentParam2));
// students.forEach(student -> System.out.println(student.getName()));
//
// System.out.println("-------");
//
// students.sort(studentComparator::compareStudentByName);
// students.forEach(student -> System.out.println(student.getName()));
//
// System.out.println("-------");
//
// students.sort(Student::compareByScore);
// students.forEach(student -> System.out.println(student.getScore()));
//
// System.out.println("-------");
//
// students.sort(Student::compareByName);
// students.forEach(student -> System.out.println(student.getName()));
//
// System.out.println("-------");
//
List<String> cities = Arrays.asList("qingdao", "chongqing", "tianjin", "shenzhen"); // Collections.sort(cities, (city1, city2) -> city1.compareToIgnoreCase(city2));
// cities.forEach(city -> System.out.println(city)); // System.out.println("-------");
//
// Collections.sort(cities, String::compareToIgnoreCase);
// cities.forEach(System.out::println);
//
// System.out.println("-------");
// }
}
public 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 compareStudentByScore(Student student1, Student student2) {
return student1.getScore() - student2.getScore();
}
public static int compareStudentByName(Student student1, Student student2) {
return student1.getName().compareToIgnoreCase(student2.getName());
}
public int compareByScore(Student student) {
return this.getScore() - student.getScore();
}
public int compareByName(Student student) {
return this.getName().compareToIgnoreCase(student.getName());
}
}
public class StudentComparator {
public int compareStudentByScore(Student student1, Student student2) {
return student1.getScore() - student2.getScore();
}
public int compareStudentByName(Student student1, Student student2) {
return student1.getName().compareToIgnoreCase(student2.getName());
}
}
Lambda can be replaced with method reference。
method reference的更多相关文章
- java中的方法引用(method reference)官方文档总结
2017/7/5 转载写明出处:http://www.cnblogs.com/daren-lin/p/java-method-reference.html 今天要说的是java中的一项新特性,方法引用 ...
- 浅析Java 8新特性Method Reference
什么是方法引用 我们知道了什么是Lambda Expression以及如何使用,那么,Method References又是什么呢?Oracle Java Docs中这样说: They are com ...
- JDK 8 - Method Reference 分析
Java SE 8 在 Java 语言层面上新增了 lambda expression 的功能,使得 Java 具备了函数式语言的能力 - 可以将函数作为方法参数传递,即 code as data. ...
- 方法引用(Method reference)和invokedynamic指令详细分析
方法引用(Method reference)和invokedynamic指令详细分析 invokedynamic是jvm指令集里面最复杂的一条.本文将详细分析invokedynamic指令是如何实现方 ...
- 方法引用(method reference)
目录 方法引用(method reference) 1. 含义 2. 分类 3. 总结 方法引用(method reference) 1. 含义 方法引用实际上是 Lambda 表达式的一种语法糖. ...
- Java中的函数式编程(四)方法引用method reference
写在前面 我们已经知道,lambda表达式是一个匿名函数,可以用lambda表达式来实现一个函数式接口. 很自然的,我们会想到类的方法也是函数,本质上和lambda表达式是一样的,那是否也可以用类 ...
- JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序
大家对java接口Comparator和Comparable都不陌生,JDK8里面Comparable还和以前一样,没有什么改动:但是Comparator在之前基础上增加了很多static和defau ...
- java 方法引用(method reference)
it -> it != null等价于Objects::nonNull
- 方法引用(Method reference)和构造器引用(construct reference)
Java 8 允许你使用 :: 关键字来传递方法或者构造函数引用 方法引用语法格式有以下三种: objectName::instanceMethod ClassName::staticMethod C ...
随机推荐
- linux-常用命令备注
//杀掉某个进程-xargs应用 ps aux | grep "udplog.js" | cut -c 9-15 | xargs kill -9 //远程拷贝文件或文件夹 sudo ...
- Which Uri Encoding method should i use in C#/.net?
June 19, 2015 This too is one of the boring "factual" posts. Sorry Lachlan. I never know w ...
- eclipse启动了tomcat,但是浏览器打不开欢迎页
tomcat在eclipse中启动成功,主页却打不开 症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他 ...
- SSH的三个组件ssh、sftp、scp介绍
SSH 包含3个组件 (1) ssh 远程登录节点 : ssh 用户名@IP地址 ① 不允许空密码或错误密码认证登录 ② 不允许root用户登录 ③ 有两个版本 ssh,ssh2安全性更高 (2) ...
- DRF中的版本控制
一.为什么要有版本 某些客户端 使用低版本只维护不开发新功能 v1 主要的产品还要不断的更新迭代功能 v2 API 版本控制允许我们在不同的客户端之间更改行为(同一个接口的不同版本会返回不同的数据). ...
- spring+hibernate中的事务
上下文: 从数据库服务器上获取数据可以,保存的时候增加了事务提交,即em.flush方法,报错no transaction in progress 报错信息: no transaction in pr ...
- mysql的innodb自增主键为什么不是连续的
图1 图1中是表t原有的数据,这个时候我们执行show create table t会看到如下输出,如图二所示现在的自增值是2,也就是下一个不指定主键值的插入的数据的主键就是2 图2 Innodb引擎 ...
- win8.1 开启企业模式
1,Win+R Gpedit.msc 2, 3, 4,F12 多了一项企业模式
- SpringCloud-Zuul搭建
一.创建工程,在pom中引入Zuul 二.重写路由加载类,实在路由的动态注册和路由转发 package com.genius.gateway.zuul; import com.genius.gatew ...
- Win7 如何阻止程序联网
https://jingyan.baidu.com/article/9113f81b03d4e12b3214c7c3.html