stream各种应用场景

public static void main(String[] args) {

List studentList1 =new ArrayList<>();

        for (int i =0; i <10; i++) {

studentList1.add(new Student(i, i%3 +"Alice"));

        }

long count = studentList1.stream().map(Student::getAge).count();

        int reduce1 = studentList1.stream().map(Student::getAge).reduce(0, Integer::sum).intValue();

        System.out.println(count==reduce1);

        //可以后排序

        List collect3 = studentList1.stream().map(Student::getName).limit(10).skip(3).sorted(String::compareTo).collect(Collectors.toList());

        List collect4 = studentList1.stream().map(Student::getName).limit(10).skip(3)

.sorted((o1, o2) -> o1.compareTo(o2)).collect(Collectors.toList());

        System.out.println(collect3);

        Student student1 =new Student(16, "Alice");

        Student student2 =new Student(16, "Bob");

        Student student3 =new Student(17, "Cherry");

        Student student4 =new Student(17, "Delivery");

        List studentList =new ArrayList<>();

        studentList.add(student1);

        studentList.add(student2);

        studentList.add(student3);

        studentList.add(student4);

        String reduce = studentList.stream().map(Student::getName).reduce("", String::concat);

        System.out.println(reduce);

        //flatMap 对象是tream

        List collect = studentList.stream().flatMap(t -> Arrays.asList(t).stream()).collect(Collectors.toList());

        System.out.println(collect);

        //map换对象

        List collect1 = studentList.stream().map(t -> ABC.builder().age(t.getAge()).build()).collect(Collectors.toList());

        System.out.println(collect);

        // List to Map

        // 1. 首先使用`Collectors.groupingBy()根据某个字段进行分组`

        Map> collect2 = studentList.stream().collect(Collectors.groupingBy(Student::getAge,

                Collectors.mapping(t -> t, Collectors.toList())));

        System.out.println(collect);

        // 2. 分组的同时使用`Collectors.mapping()`将分组后的数据组织起来

        Map> map = studentList.stream()

.collect(

Collectors.groupingBy(Student::getAge,

                                Collectors.mapping(Student::getName, Collectors.toList())));

        System.out.println(map);

        // 这里将分组后的数据组织成一个Map

        Map> map2 = studentList.stream()

.collect(

Collectors.groupingBy(Student::getAge,

                                Collectors.toMap(Student::getName, student -> student)));

        System.out.println(map2);

    }

}

class Student {

private Stringname;

    private Integerage;

    Student(Integer age, String name) {

this.age = age;

        this.name = name;

    }

public IntegergetAge() {

return age;

    }

public void setAge(Integer age) {

this.age = age;

    }

public StringgetName() {

System.out.println(name);

        return name;

    }

public void setName(String name) {

this.name = name;

    }

@Override

    public StringtoString() {

return "Student{" +

"name='" +name +'\'' +

", age=" +age +

'}';

    }

}

@Builder

@Getter

@Setter

@ToString

class ABC {

private Stringname;

    private Integerage;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容