Say I have a class of Student contain in fields : firstName and surname
I then use this to create two lists
List<Student> classroomA = {["Ben","oreilly"], ["Jenna","Birch"]}
List<Student> classroomB = {["Alan","Messing"], ["Ben", "Mancini"], ["Helena","Wong"]}
How would I go about using these lists to get all Students with same name from the list :
List<Student> commonStudents = {["Ben","oreilly"],["Ben", "Mancini"]}
Would doing for loop on both list and doing a classroomA.getfirstName().equals(classroomB.getfirstName())
the only way ?
Below code gets all Bens from the list. If you want a particular field from the object (which is transformation), then use the map on filter stream.
List<Student> AllBens = classA.stream().filter(Objects::nonNull).
filter(k -> StringUtils.isNotEmpty(k.getName()) && k.getName().equalsIgnoreCase("Ben")).collect(Collectors.toList());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.