语法 ()->{}
Lambda表达式不是替代匿名内部类,因为编译后没有生成内部类的class文件。
1,多线程示例:
java8以前的多线程写法
package com.wjb.lambda;
/**
* Created by ptdy on 2017/5/17.
*/
public class ThreadDemo1 implements Runnable{
@Override
public void run() {
for(int i = 0;i<100;i++){
System.out.println("run:"+i);
}
}
public static void main(String[] args) {
Thread thread = new Thread(new ThreadDemo1());
thread.start();
for(int i = 0;i<100;i++){
System.out.println("main:"+i);
}
}
}
java8以前匿名内部类的写法
package com.wjb.lambda;
/**
* Created by ptdy on 2017/5/17.
*/
public class ThreadDemo2 {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0;i < 100;i++){
System.out.println("run:"+i);
}
}
}).start();
for(int x = 0;x<100;x++){
System.out.println("main:"+x);
}
}
}
java8的Lambda表达式写法
package com.wjb.lambda;
/**
* Created by ptdy on 2017/5/17.
*/
public class ThreadDemo3 {
public static void main(String[] args) {
Runnable runnable = ()->{
for(int i = 0;i < 100;i++){
System.out.println("run..."+i);
}
};
new Thread(runnable).start();
for (int i = 0;i < 100;i ++){
System.out.println("main..."+i);
}
}
}
2,TreeSet排序示例
java8以前的写法
package com.wjb.lambda;
import java.util.Comparator;
import java.util.TreeSet;
/**
* Created by ptdy on 2017/5/17.
*/
public class TreeSetLambda1 {
public static void main(String[] args) {
TreeSet treeSet = new TreeSet(new StringLength());
treeSet.add("a");
treeSet.add("eb");
treeSet.add("vbc");
treeSet.add("badsds");
System.out.println(treeSet);
}
static class StringLength implements Comparator<String>{
@Override
public int compare(String o1, String o2) {
int length = o1.length() - o2.length();
return length == 0 ? o1.compareTo(o2):length;
}
}
}
java8以前的匿名内部类写法
package com.wjb.lambda;
import java.util.Comparator;
import java.util.TreeSet;
/**
* Created by ptdy on 2017/5/17.
*/
public class TreeSetLambda2 {
public static void main(String[] args) {
TreeSet treeSet = new TreeSet(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int length = o1.length() - o2.length();
return length == 0 ? o1.compareTo(o2):length;
}
});
treeSet.add("a");
treeSet.add("eb");
treeSet.add("vbc");
treeSet.add("badsds");
System.out.println(treeSet);
}
}
java8的Lambda写法
package com.wjb.lambda;
import java.util.Set;
import java.util.TreeSet;
/**
* Created by ptdy on 2017/5/17.
*/
public class TreeSetLambda3 {
public static void main(String[] args) {
Set<String> treeSet = new TreeSet<String>((s1, s2)->{
int length = s1.length() - s2.length();
return length == 0 ? s1.compareTo(s2):length;
});
treeSet.add("a");
treeSet.add("eb");
treeSet.add("vbc");
treeSet.add("badsds");
System.out.println(treeSet);
}
}
3,F盘文件名过滤
java8以前写法
package com.wjb.lambda;
import java.io.File;
import java.io.FileFilter;
/**
* Created by ptdy on 2017/5/17.
*/
public class FileLambda1 {
public static class FileTxt implements FileFilter{
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".txt");
}
}
public static void main(String[] args) {
File file = new File("F:\\demo");
File[] files = file.listFiles(new FileTxt()); //获取demo目录下的所有文件名
for (File f:files){
System.out.println(f);
}
}
}
java8的Lambda写法
package com.wjb.lambda;
import java.io.File;
/**
* Created by ptdy on 2017/5/17.
*/
public class FileLambda1 {
public static void main(String[] args) {
File file = new File("F:\\demo");
File[] files = file.listFiles((pathname)->{
return pathname.getName().endsWith(".txt");
});
for (File f:files){
System.out.println(f);
}
}
}
网友评论