jdk1.8中stream练习

jdk1.8中stream练习

public class stream {
    public static void main(String[] args) {
        List<Student> students = initData();
        //判断对象是否为空
//        Student student = new Student();
//        if(Optional.ofNullable(student).isPresent()){
//            System.out.println("不为空");
//        }{
//            System.out.println("为空"); }
        //Stream API中的方法并不会影响原始集合
        streamImpl(students);
        java7Impl(students);
//        String[] arr ={"aa","ccc","sss"};
//        System.out.println(Arrays.stream(arr).collect(joining()));
//        // aacccsss
//        System.out.println(Arrays.stream(arr).collect(joining("|")));
//        //aa,ccc,sss
//        System.out.println(Arrays.stream(arr).collect(joining(",")));
//        // aa|ccc|sss
//        System.out.println(Arrays.stream(arr).collect(joining(",","{","}")));
//        // [aa, ccc, sss]  数组转成list
//        System.out.println(Arrays.stream(arr).collect(toList()));

    }

    public static void streamImpl(List<Student> students){
        //转换成map
        Map<Double, Long> collect = students.stream().collect(groupingBy(Student::getScore, counting()));
        for (Map.Entry<Double, Long> map :collect.entrySet()){
            System.out.println("key:"+map.getKey()+"value:"+map.getValue());
        }
        //用stream流遍历score>60的人      对象.stream  进行过滤    过滤条件
        List<Student>filterStudent  = students.stream().filter(one -> one.getScore() < 60).collect(toList());
        filterStudent.stream().forEach(System.out::println);
        List<Student> collect1 = students.stream().filter(one -> one.getScore() < 60).collect(Collectors.toList());
        collect1.stream().forEach(System.out::println);
        //排序
        List<Student> collect2 = students.stream().sorted(Comparator.comparing(Student::getScore)).collect(toList());
        collect2.stream().forEach(System.out::println);
                                            //对象  属性       按照  id排序                倒叙   默认不写正序
        students.sort(Comparator.comparing(Student::getScore).thenComparing(Student::getId).reversed());
        //总成绩
        int sum = students.stream().mapToInt(scoreTo -> (int) scoreTo.getScore()).sum();
        System.out.println(sum);

    }
    //1.7的方式
    public static void java7Impl(List<Student> students) {
        List<Student> filterStudent = new ArrayList<>();
        for (Student student : students) {
            if (student.getScore() < 60) {
                filterStudent.add(student);
            }
        }
        System.out.println(filterStudent);
    }

    public static List<Student> initData() {
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三", 60));
        students.add(new Student("李四", 80));
        students.add(new Student("王五", 50));
        students.add(new Student("赵六", 70));
        students.add(new Student("孙七", 30));
        students.add(new Student("周八", 30));
        return students;
    }
    @Data
    static class Student {
        /**
         * 学生姓名
         */
        private String name;

        /**
         * 学生分数
         */
        private double score;

        /**
         * 所学课程
         */
        List<String> course;

        public Student(int i, String 张三, int i1) {
        }

        public Student(String name, double score) {
            this.name = name;
            this.score = score;
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值