java8学习

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));
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容