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()根據(jù)某個字段進(jìn)行分組`
? ? ? ? Map> collect2 = studentList.stream().collect(Collectors.groupingBy(Student::getAge,
? ? ? ? ? ? ? ? Collectors.mapping(t -> t, Collectors.toList())));
? ? ? ? System.out.println(collect);
? ? ? ? // 2. 分組的同時使用`Collectors.mapping()`將分組后的數(shù)據(jù)組織起來
? ? ? ? Map> map = studentList.stream()
.collect(
Collectors.groupingBy(Student::getAge,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Collectors.mapping(Student::getName, Collectors.toList())));
? ? ? ? System.out.println(map);
? ? ? ? // 這里將分組后的數(shù)據(jù)組織成一個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;
}