Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch Student object from List<Student> with matching name without iterating (Java)

I have a List<Student> and String firstName="George". I want to extract Student object from List matching with "George" without iterating list. Is it possible?

May be if some method override required or anything to make it work.

public Class Student {
     private String firstName;
     private String lastName;
     private String class;

     // getter, setter methods goes here....
}

Please help me thanks.

like image 367
changeme Avatar asked Nov 10 '22 07:11

changeme


1 Answers

If you are using Java 8 then its possible using Stream and Lambda:-

List<Student> myStudent = studentsList.stream()
                                      .filter(s -> s.name.equals("George"))
                                      .collect(Collectors.toList());
like image 85
Vineet Tyagi Avatar answered Nov 15 '22 07:11

Vineet Tyagi