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的更多相关文章

  1. java中的方法引用(method reference)官方文档总结

    2017/7/5 转载写明出处:http://www.cnblogs.com/daren-lin/p/java-method-reference.html 今天要说的是java中的一项新特性,方法引用 ...

  2. 浅析Java 8新特性Method Reference

    什么是方法引用 我们知道了什么是Lambda Expression以及如何使用,那么,Method References又是什么呢?Oracle Java Docs中这样说: They are com ...

  3. JDK 8 - Method Reference 分析

    Java SE 8 在 Java 语言层面上新增了 lambda expression 的功能,使得 Java 具备了函数式语言的能力 - 可以将函数作为方法参数传递,即 code as data. ...

  4. 方法引用(Method reference)和invokedynamic指令详细分析

    方法引用(Method reference)和invokedynamic指令详细分析 invokedynamic是jvm指令集里面最复杂的一条.本文将详细分析invokedynamic指令是如何实现方 ...

  5. 方法引用(method reference)

    目录 方法引用(method reference) 1. 含义 2. 分类 3. 总结 方法引用(method reference) 1. 含义 方法引用实际上是 Lambda 表达式的一种语法糖. ...

  6. Java中的函数式编程(四)方法引用method reference

    写在前面 我们已经知道,lambda表达式是一个匿名函数,可以用lambda表达式来实现一个函数式接口.   很自然的,我们会想到类的方法也是函数,本质上和lambda表达式是一样的,那是否也可以用类 ...

  7. JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序

    大家对java接口Comparator和Comparable都不陌生,JDK8里面Comparable还和以前一样,没有什么改动:但是Comparator在之前基础上增加了很多static和defau ...

  8. java 方法引用(method reference)

    it -> it != null等价于Objects::nonNull

  9. 方法引用(Method reference)和构造器引用(construct reference)

    Java 8 允许你使用 :: 关键字来传递方法或者构造函数引用 方法引用语法格式有以下三种: objectName::instanceMethod ClassName::staticMethod C ...

随机推荐

  1. QGIS编译教程

    注意更新时间:Thursday November 02, 2017 1. Introduction 简介 This document is the original installation guid ...

  2. Android内置和外置SD卡的位置获取

    public class StorageUtils { private static String TAG="123"; // 获取主存储卡路径 内置内存卡路径 public st ...

  3. HTML5游戏开发进阶指南 中文pdf扫描版​

    HTML5游戏开发进阶指南介绍了HTML5游戏开发的一般过程和技巧.全书共分12章,第1章介绍了本书相关的HTML5的诸多新特性,包括在canvas上绘图.播放声音等,另外还引入了子画面页的概念:第2 ...

  4. centos7下git的安装和配置

    git的安装: yum 源仓库里的 Git 版本更新不及时,最新版本的 Git 是 1.8.3.1,但是官方最新版本已经到了 2.9.2.想要安装最新版本的的 Git,只能下载源码进行安装. 1. 查 ...

  5. MultiDataTrigger

    MultiDataTrigger是多条件数据触发器 和MltiTrigger是同样的,只不过前者是数据,后者是属性. 这个是基本的使用语法 <MultiDataTrigger> <M ...

  6. openvpn的搭建与应用

    一.VPN概述: VPN(Virtual Private NetWork,虚拟专用网络)架设在公共共享的基础设施互联网上,在非信任的网络上建立私有的安全的连接,把分布在不同地域的办公场所.用户或者商业 ...

  7. iOS开发系统类功能划分

    0.OC语法基础 CHOCBase Object C语法学习笔记(一) Object C语法学习笔记(二) 1.UI类 自定义控件程序运行流程 setNeedsLayOut和setNeedsDispl ...

  8. 模块-os.system的两个模块/random模块/datetime模块/写日志

    一.获取当前目录的路径 os.path.abspath('.')# 取绝对路径 os.getcwd()# 取当前路径 .代表当前目录 ..上一级目录 ../.. 二.执行操作系统命令1.os.syst ...

  9. Nagios监控平台搭建及配置文件详解

    Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员, ...

  10. (原創) Gvim 個人習慣常用設定 (vim)

    不定期更新這篇,因為查詢到好用的設定或者插件就會更新自己的設定. "set nocompatible let $LANG='zh_TW.UTF-8' set langmenu=zh_tw.u ...