Indendation, spacing & visibility do not follow current industry requirements.
Furthermore, now that there is records (since 2 releases), there is no point in showing an anemic POJO with not-private fields (why wrote accessors in this case ?).
Finally, the Java Stream API is designed to introduce functionnal programming, paradigm in which we do not want side effect.
To avoid side effect, for starters, do not use mutable data objects.
public static void main(String args[]) { record Student(String name, int age) {}
List<Student> listOfStudents = List.of( new Student("Anchit", 20), new Student("Peter", 19), new Student("Martin", 22), new Student("Sam", 21) );
/* Using map function to convert Stream<Student> to Stream<String> */ List<String> listOfStudentNames = listOfStudents.stream() .map(s -> s.name()) .collect(Collectors.toList());
Indendation, spacing & visibility do not follow current industry requirements.
Furthermore, now that there is records (since 2 releases), there is no point in showing an anemic POJO with not-private fields (why wrote accessors in this case ?).
Finally, the Java Stream API is designed to introduce functionnal programming, paradigm in which we do not want side effect.
To avoid side effect, for starters, do not use mutable data objects.
In conclusion, the example can be rewritten :