jdk8中一个重要特性Stream流

Stream的一些用法

1.java8除了lambda新特性,还有一个容易忽视的Stream流:

List<Student> studentList=StudentData.getStudents();
        Stream<Student> stream=studentList.stream();//返回一个顺序流
        Stream<Student> stream2=studentList.parallelStream();//创建一个并行流
        //通过一个数组创建stream
        int []arr=new int[]{1,2,3,45,69,78};
        IntStream intStream=Arrays.stream(arr);
        //通过stream.of
        Stream<Integer> stream4=Stream.of(1,2,6,9,5);
        Stream<Student>studentStream=Stream.of(new Student(1,"刘备",2,44.25));
        //创建一个无限流
        Stream.iterate(0,t->t+5).forEach(System.out::println);//每隔5个数取一个,无限循环
        Stream.iterate(0,t->t+5).limit(5).forEach(System.out::println);//每隔5个数取一个,只取前5个
        Stream.generate(Math::random).limit(5).forEach(System.out::println);//取出一个随机数
        studentList.stream().filter(item->item.getAge()>20).forEach(System.out::println);
        studentList.stream().limit(3).forEach(System.out::println);
        studentList.stream().skip(4).forEach(System.out::println);//跳过前4个元素
        //过滤重复的元素
        studentList.stream().distinct().forEach(System.out::println);
        映射
        Stream<Student>stream6=studentList.stream();
        Stream<Integer>ageStream=stream6.map(Student::getAge);
        ageStream.filter(age->age>50).forEach(System.out::println);
        List<Integer>list2=Arrays.asList(1,5,9,6,25,25,58,36);
        //归约 计算总和
        list2.stream().reduce(Integer::sum);
        list2.stream().sorted().forEach(System.out::println);//自然排序
        studentList.stream().sorted().forEach(System.out::println);
        studentList.stream().sorted((e1,e2)->Integer.compare(e1.getAge(),e2.getAge())).forEach(System.out::println);//从小到大
        //匹配和查找
        boolean s=studentList.stream().allMatch(item->item.getAge()<80);//查找所有年龄匹配给出条件的结果
        //System.out.println(s);
        boolean anymatch=studentList.stream().anyMatch(item->item.getAge()==69);
        boolean noneMatch=studentList.stream().noneMatch(item->item.getName().equals("关羽"));
        Optional<Student>first=studentList.stream().findFirst();
        System.out.println(first);
        //收集
        List<Student>list8=studentList.stream().filter(e->e.getAge()>50).collect(Collectors.toList());
        Set<Student>list9=studentList.stream().filter(e->e.getAge()>50).collect(Collectors.toSet());
        System.out.println(list8.size());
        list2.stream().distinct().forEach(System.out::println);

2.其中getStudents()方法是一个静态的获取学生信息集合的方法

public static List<Student> getStudents(){
        List<Student> lists=new ArrayList<>();
        lists.add(new Student(1,"刘备",69,90.4));
        lists.add(new Student(1,"刘备",69,90.4));
        lists.add(new Student(3,"关羽",45,90.4));
        lists.add(new Student(4,"赵云",18,90.4));
        lists.add(new Student(5,"曹操",32,90.4));
        return  lists;

    }

3.Student类事业观为了测试方便所建学生类

private  Integer id;
    private  String name;
    private  Integer age;
    private  Double score;

    public Student(Integer id, String name, Integer age, Double score) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.score = score;
    }
    //...省略get,set方法和toString()方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值