其他分享
首页 > 其他分享> > lambda 之 forEach( ->{} )

lambda 之 forEach( ->{} )

作者:互联网

1.Student类

public class Student {
    private String name;
    private Integer age;

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

    public String getName(){
        return name;
    }

    public Integer getAge(){
        return age;
    }

}

2.Foreach类

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sk on 2021/1/13
 */
public class Foreach {

    public static void main(String[] args) {

        Student student1 = new Student();
        Student student2 = new Student();

        List<Student> students = new ArrayList<>();
        student1.setName("小王");
        student1.setAge(9);
        student2.setName("小李");
        student2.setAge(2);
        students.add(student1);
        students.add(student2);

        String i = "小王";

        students.forEach(s -> {
            if (s.getName().equals(i)) {
                    s.setAge(19);
            }
            System.out.println(s.getAge());
            System.out.println(s.getName());
            System.out.println("------------------------");
        });
    }
}

3.执行结果

 

标签:String,student2,age,forEach,Student,lambda,public,name
来源: https://www.cnblogs.com/songxiaoke/p/14271851.html