I am new to Java8 and want to learn effective ways to use different features introduced. I created this simple program with 2 classes and tried to use forEach loop. But inside the forEach loop I am not sure if I can in anyway use method reference or filter in the below scenario with streams or what I have coded is fine. Any suggestion/help is appreciated. Thank you!
public class Student {
private String name;
private int rollNo;
private String major;
public Student(String name, int rollNo, String major) {
this.name = name;
this.rollNo = rollNo;
this.major = major;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Student student1 = new Student("Raj", 1, "commerce");
Student student2 = new Student("Ram", 2, "commerce");
Student student3 = new Student("Riya", 3, "pcb");
Student student4 = new Student("Rita", 4, "pcm");
List<Student> studentList_1 = new ArrayList<>();
studentList_1.add(student1);
studentList_1.add(student2);
studentList_1.add(student3);
studentList_1.add(student4);
List<Student> studentList_2 = new ArrayList<>();
studentList_2.add(student1);
studentList_2.add(student2);
studentList_2.add(student3);
studentList_2.forEach(student ->
{
//this is where I want to check if I can use any of the Java 8 features
if(studentList_1.contains(student)) {
boolean isEligibleForDoctor = getEligibility(student.getMajor());
if(isEligibleForDoctor) {
System.out.println("Eligible for doctor :: "+student.getName());
}
}
});
}
private static boolean getEligibility(String major) {
if(major.equals("pcb")) {
return true;
}else {
return false;
}
}
}