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. Azure 执行模型

    最后更新时间(英文版):01/20/2015 最后更新时间(中文版):04/11/2015 Azure 提供了用于运行应用程序的不同执行模型.每种模型提供一组不同服务,而你选择哪种模型完全取决于你要做 ...

  2. AutoLayout自动布局之VFL语言代码实现(一个神奇的语言)

    一.什么是VFL语言?为什么要VFL语言? VFL全称是Visual Format Language,翻译过来是“可视化格式语言” VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言 ...

  3. easyUI Methods

    doc对象转jQuery 对象 $(doc Object); jQuery Object.控件名('方法'[,参数]); options 为该控件的属性 方式一: var opts = $('.eas ...

  4. Java WEB中的HttpServletResponse数据传递

    1.什么是HttpServletResponse 2.使用HttpServletResponse向浏览器发送数据及相关实例. 实例1:实现文件下载功能 实例2:实现验证码注册 实例3:实现页面3秒后跳 ...

  5. 解决jquery操作checkbox火狐下第二次无法勾选问题

    最近在学习jQuery(版本jquery-1.9.1.js),要求用jQuery实现全选/全不选.反选,在IE(IE8)中没有问题,但在火狐浏览器中调试的时候出现了一些小问题,达不到效果. html代 ...

  6. Python htmlTestRunner生成测试报告Demo

    #该代码段是ReadTxt_demo.py 的代码,用户读取txt 文件中的用户信息. #ReadTxt_demo.py def readTxt(filePath): fo = open(filePa ...

  7. BZOJ 3083 遥远的国度(树链剖分+LCA)

    Description 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要z ...

  8. jQuery高级

    一.动画效果 常用的几种效果都是没有easing参数的,也即动画只能swing.$(selector).animate(styles,speed,easing,callback)中是有easing参数 ...

  9. 【转】开源视频录制库LandscapeVideoCamera

    非常强大的android 视频录制库,可以选择视频尺寸以及视频质量,只允许横屏录制. 使用Android自带的Camera应用可以录制视频,只需发送MediaStore.ACTION_VIDEO_CA ...

  10. angular学习文章

    https://www.jianshu.com/p/86c6249a2069 angular.cn https://segmentfault.com/a/1190000008754631