Update java lambda expression guilde (#19102)

This commit is contained in:
Anh N. Nguyen
2018-10-16 07:57:27 +07:00
committed by Manish Giri
parent ee05051613
commit 57d59d5a77

View File

@ -14,7 +14,7 @@ The Stream Api is used in java to allow chaining of sequential and aggregate ope
In this small example you can see that one of the utilities of a stream is to receive a certain property of all objects in a list and return it in another list using intermediate and terminal operations. In this small example you can see that one of the utilities of a stream is to receive a certain property of all objects in a list and return it in another list using intermediate and terminal operations.
Assume you have an object class of Student. Assume you have an object class of Student.
``java ```java
public class Student { public class Student {
int studentId; int studentId;
String studentName; String studentName;
@ -28,27 +28,27 @@ public class Student {
} }
// setters // setters
} }
`` ```
Now assume in some method you have a list of all the students and want to get a list of all the student names. Now assume in some method you have a list of all the students and want to get a list of all the student names.
Traditionally this can looks something like this. Traditionally this can looks something like this.
``java ```java
List<Student> students = some list of student objects List<Student> students = some list of student objects
List<String> studentNames = new ArrayList<>(); List<String> studentNames = new ArrayList<>();
for(Student student: students) { for(Student student: students) {
studentNames.add(student.getStudentName()); studentNames.add(student.getStudentName());
} }
`` ```
While this isn't terrible it can be simplified. While this isn't terrible it can be simplified.
Using streams this is possible with one line of code. Using streams this is possible with one line of code.
``java ```java
List<Student> students = some list of student objects List<Student> students = some list of student objects
List<String> studentNames = students.stream().map(String::getStudentName).collect(Collectors.toList()); List<String> studentNames = students.stream().map(String::getStudentName).collect(Collectors.toList());
`` ```
The students stream api goes over the list of students and uses the intermediate map function to return a new list of streams using whatever method is on the right of the :: The students stream api goes over the list of students and uses the intermediate map function to return a new list of streams using whatever method is on the right of the ::