java8学习

2019-05-05  本文已影响0人  sindorina

1.Lambda表达式

Arrays.asList("a","b","c").forEach(e->Log.i("tag","e-->"+e));//forEach需要API 24

2.java8 Interface中支持有实现的方法

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);//静态方法的引用

//默认方法 − 默认方法就是一个在接口里面有了一个实现的方法。
@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);//调用无参构造
 List<Student> list = Arrays.asList(student);
 list.forEach(student::signStudent);

4.stream
map 方法用于映射每个元素到对应的结果

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));
上一篇 下一篇

猜你喜欢

热点阅读