jdk8 stream map(),方法用于映射每个元素到对应的结果,其中->右边得到的结果会做为下一步操作的初始值

package com.xiangshuai.lambda;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lqx
 * @ClassName StreamMapFunction
 * @description
 * @date 2020/8/18 11:16
 */
public class StreamMapFunction {
    public static void main(String[] args) {
        List Student1s=new ArrayList<>();
        Student1s.add(new Student1(10,"小明","ss"));
        Student1s.add(new Student1(30,"小明","ss"));
        Student1s.add(new Student1(20,"小明","ss"));
        Student1s.add(new Student1(90,"小明","ss"));
        Student1s.add(new Student1(200,"小明","ss"));
        Student1s.add(new Student1(80,"小明","ss"));
        Student1s.add(new Student1(1,"小明","ss"));
        Student1s.add(new Student1(1,"小红","ss"));
        Collections.sort(Student1s, Student1::compareAge);
        // System.out.println(Student1s);
        List collect = Student1s.stream().filter(Student1 -> "小明".equals(Student1.getName()))
                //map后面的collect操作的值其实是map ->右边即Student1.setAge2方法的返回值
                .map(Student1 -> Student1.setAge2(Student1.getAge() + 1))
                .collect(Collectors.toList());
        collect.forEach(Student1 -> {
            System.out.println(Student1);
        } );
    }
}

class Student1 {
    private Integer age;
    private String name;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public Student1 setAge2(Integer age) {
        this.setAge(age);
        return  this;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public static int compareAge(Student1 s1, Student1 s2){
        return s1.getAge().compareTo(s2.getAge());
    }

    public static int compareAgeStatic(Student1 s1, Student1 s2){
        return  s1.getAge().compareTo(s2.getAge());
    }

    public Student1(Integer age, String name, String address) {
        this.age = age;
        this.name = name;
        this.address = address;
    }

    public Student1() {
    }

    @Override
    public String toString() {
        return "Student1{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

你可能感兴趣的:(J2SE)