We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Aash • 2 years ago

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 :

package org.tech.io;

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

public class StreamMapMain {

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())


Anonymous • 2 years ago