I have a class say Student. I want to use Java Stream API multiple times to map to multiple properties. I want to use the Stream to map for name and with the age . How this can be done?
public class Student {
String name;
int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
I want to use something like this
public static List<Student> createListOfStudents()
{
List<Student> listOfStudents=new ArrayList<>();
Student s1= new Student("Anchit",20);
Student s2= new Student("Peter",19);
Student s3= new Student("Martin",22);
Student s4= new Student("Sam",21);
listOfStudents.add(s1);
listOfStudents.add(s2);
listOfStudents.add(s3);
listOfStudents.add(s4);
return listOfStudents;
}
List<Student> listOfStudents = createListOfStudents();
listOfStudents.stream().map(x -> x.getName()).map(x->x.getAge());
How can this be achieved