To get list of member from list of objects using Streams java
11,015
You need to use Stream#map
method, something like this:
List<Student> students = ...;
List<String> names = students.stream().map(Student::getName).collect(Collectors.toList());
Author by
user3425563
Updated on June 15, 2022Comments
-
user3425563 almost 2 years
I have a class with two members and a list of objects of that class . Now I want to extract list of members from the list of objects.
e.g :
class student { int Id; String studentName; }
now I need to retrieve a list of studentName, from list of students. How can this be done with java8 Streams?
Solution without using streams:
List<student> studentList; List<String> nameList = new ArrayList<String>(); Iterator iterator = studentList.iterator(); while(iterator.hasNext()){ nameList.add(iterator.next().getStudentName()); }
-
user3425563 about 7 yearsthanks . it worked
-
Anton Balaniuc about 7 years@user3425563, remember to accept and up-vote one of the answers, if you find it useful.