今天看了下 java 8 的新特性 lambda 表达式感觉用起来简洁 但................

哎呦-又忘了 发布于 2016/09/27 14:30
阅读 234
收藏 0
package Demo1;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;


public class Demo {
public static void main(String[] args) {
List<String> list=Arrays.asList("1","2","3","7","8");
list.forEach(a->{System.out.println(a);});
C c1=(x,y)->x*y;
System.out.println(c1.add(2, 5) );

list.stream().filter( new Predicate<String>(){
public boolean test(String t) {
return t!="3";
}
}).map(new Function<String, Integer>() {
public Integer apply(String t) {
return new Integer(t);
}
}).distinct().collect(()->new ArrayList<Integer>(),(a,b)->{// 用匿名内部类怎么玩?
a.add(b);
},(a,b)->{
a.addAll(b);
}).forEach(e->
{
System.out.print(e);
System.out.println("\t");
}
);
System.out.println("\t");

// <R> R collect(Supplier<R> supplier,BiConsumer<R, ? super T> accumulator,BiConsumer<R, R> combiner);
List<String> list2=Arrays.asList("1","2","3","7","8");
List<Integer> result=list2.stream().map(a->new Integer(a)).filter(a-> a!=3).distinct()
//.collect(ArrayList::new, ArrayList::add,ArrayList::addAll);
.collect(() -> new ArrayList<Integer>(),(a, b) -> a.add(b),(c, d) -> c.addAll(d));// 后两个 表达式该如何理解?
result.forEach(e->
{
System.out.print(e);
// ..................
}
);
}

public int add(int a,int b){
return a+b;
}
}




abstract class B{
abstract public int add(int a,int b);
}


@FunctionalInterface
interface C{
public int add(int a,int b);
}


加载中
0
skhuhu
skhuhu
简介 不易阅读·····
返回顶部
顶部