I have an ArrayList of objects called Student.
I want to filter this list and return a single Student object only based on hierarchical conditions.
The conditions are as follows:
- Select the first COMP student.
- If none are returned, select an ECON student.
- If none are returned, select a AGRI student.
- ... etc.
In my design, I have hard coded this - but maybe it can use a sorted Set or List or something.
My current code works, but seems messy, and will grow quite large if more conditions are introduced.
I need help with the Student filterStudent(List<Student> studentList) method. How to do this in fewer lines of code maybe or method chaining. And how to introduce a sorted Set of Enums that can be filtered based on preference (Example, COMP first, if empty, then ECON next, etc. Stop filtering if one is found).
Code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
class Student {
private final int stNumber;
private final String stModule;//...more fields
public Student(int stNumber, String stModule) {
this.stNumber = stNumber;
this.stModule = stModule;
}
public int getStNumber() {
return stNumber;
}
public String getStModule() {
return stModule;
}
@Override
public String toString() {
return "Student{" +
"stNumber=" + stNumber +
", stModule='" + stModule + '\'' +
'}';
}
}
enum Module {
COMP("Computers"),
ECON("Economics"),
AGRI("Agriculture"),
PHYS("Physics"); //...very large list
private String moduleName;
public String getModuleName() {
return moduleName;
}
Module(String moduleName) {
this.moduleName = moduleName;
}
}
public class Question {
public static void main(String[] args) throws Exception {
//These lists can be any size and contain any information
List<Student> studentListOne = Arrays.asList(
new Student(41, "Economics"),
new Student(45, "Computers")
);
List<Student> studentListTwo = Arrays.asList(
new Student(11, "Physics"),
new Student(23, "Agriculture"),
new Student(86, "Physics"),
new Student(34, "Economics")
);
List<Student> studentListThree = new ArrayList<>();
System.out.println(filterStudent(studentListOne));
System.out.println(filterStudent(studentListTwo));
System.out.println(filterStudent(studentListThree)); //exception
}
/**
* Rules are: Choose the first instance of a COMP student.
* If the list is empty, choose a ECON student.
* If the list is still empty, choose a AGRI student.
*
* @param studentList
* @return Student
*/
public static Student filterStudent(List<Student> studentList) throws Exception {
Optional<Student> selectedStudent;
selectedStudent = studentList.stream().filter(student -> student.getStModule().equalsIgnoreCase(Module.COMP.getModuleName())).findFirst();
if (selectedStudent.isPresent()) {
return selectedStudent.get();
}
selectedStudent = studentList.stream().filter(student -> student.getStModule().equalsIgnoreCase(Module.ECON.getModuleName())).findFirst();
if (selectedStudent.isPresent()) {
return selectedStudent.get();
}
selectedStudent = studentList.stream().filter(student -> student.getStModule().equalsIgnoreCase(Module.AGRI.getModuleName())).findFirst();
if (selectedStudent.isPresent()) {
return selectedStudent.get();
}
throw new Exception("No student found matching criteria");
}
}