public class Array {
public static void main(String[] args) {
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee("tanaka", 25));
list.add(new Employee("yamada", 28));
list.add(new Employee("suzuki", 20));
list.forEach( s -> System.out.println(s.getName() + ":" + s.getAge()));
}
}
When there is one repeating execution process, it can be written in one line like this, but when there are multiple execution processes, enclose the execution process with {}
list.forEach( s -> {
System.out.println(s.getName());
System.out.println(s.getAge());
});
The way to write without using a lambda expression is as follows
list.forEach(new Consumer<Employee>() {
@Override
public void accept(Employee e) {
System.out.println(e.getName() + " : " + e.getAge());
}
});
Recommended Posts