标签:java
对于Lambda的解释在wiki百科看到了就不做翻译保留原味(看看大家的理解,有不同的看法欢迎探讨)
Lambda (programming), a function (or a subroutine) defined, and possibly called, without being bound to an identifier
Lambda expression in Lambda calculus, a formal system in mathematical logic and computer science for expressing computation by way of variable binding and substitution
来自: http://medianetwork.oracle.com/video/player/3373491280001
2. 例子
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class LambdaExpresstionTouris {
public static void main(String[] args) {
List<String> ocuppys1 = new ArrayList<String>();
ocuppys1.add("bussinessman");
ocuppys1.add("doctor");
ocuppys1.add("teacher");
ocuppys1.add("programer");
ocuppys1.add("stuedent");
List<String> ocuppys2 = new ArrayList<String>();
ocuppys2.add("bussinessman");
ocuppys2.add("doctor");
ocuppys2.add("teacher");
ocuppys2.add("programer");
ocuppys2.add("stuedent");
LambdaExpresstionTouris leTouris = new LambdaExpresstionTouris();
System.out.println("use java 7 sort:");
System.out.println("before:"+ocuppys1);
leTouris.sortByJava7(ocuppys1);
System.out.println("before:"+ocuppys1);
System.out.println("use java 8 sort:");
System.out.println("before:"+ocuppys2);
leTouris.sortByJava8(ocuppys2);
System.out.println("before:"+ocuppys2);
}
//使用JAVA 7的原始排序方法
private void sortByJava7(List<String> listStr){
Collections.sort(listStr, new Comparator<String>() {
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
}
//使用JAVA 8 lambda 表达式进行排序
private void sortByJava8(List<String> listStr){
Collections.sort(listStr,(s1,s2) -> s1.compareTo(s2));
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:java
原文地址:http://blog.csdn.net/luxideyao/article/details/47135573