1.Lambda表達(dá)式
Arrays.asList("a","b","c").forEach(e->Log.i("tag","e-->"+e));//forEach需要API 24
2.java8 Interface中支持有實(shí)現(xiàn)的方法
public interface IStudent {
default String getName(){
return "xiaoMing";
}
}
3.方法的引用
List<String> names1 = new ArrayList<String>();
names1.add("Google ");
names1.add("Runoob ");
names1.add("Taobao ");
names1.add("Baidu ");
names1.add("Sina ");
Collections.sort(names1, String::compareTo);//靜態(tài)方法的引用
//默認(rèn)方法 ? 默認(rèn)方法就是一個(gè)在接口里面有了一個(gè)實(shí)現(xiàn)的方法练慕。
@FunctionalInterface
public interface IFunctional {
void get();
}
public class Student{
public static Student addStudent(IFunctional<Student> iFunctional){
return iFunctional.get();
}
public void signStudent(Student student){
Log.i("tag","登記成功");
}
}
Student student = Student.addStudent(Student::new);//調(diào)用無參構(gòu)造
List<Student> list = Arrays.asList(student);
list.forEach(student::signStudent);
4.stream
map 方法用于映射每個(gè)元素到對(duì)應(yīng)的結(jié)果
List<Integer>intList = Arrays.asList(1,2,3,4,7,10);
List<Integer> s= intList.stream().map(e->e*2+1).collect(Collectors.toList());
filter過濾
long count = intList.stream().filter(x -> x == 2 ).count();
limite
Random random = new Random();
random.ints().limit(10).forEach(e->Log.i("tag","e-->"+e));