Skip to content

Instantly share code, notes, and snippets.

@patio11
Created September 21, 2012 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patio11/3760402 to your computer and use it in GitHub Desktop.
Save patio11/3760402 to your computer and use it in GitHub Desktop.
Java can be fun!
List<Student> studentsWhoHaveNotTakenAtLeastOneTest() {
List<Student> allStudents = this.getStudents().clone(); /* shallow copy to avoid mutation */
Iterator<Student> studentIt = allStudents.iterator();
/* select down to students missing at least one test */
List<Student> studentsMissingAtLeastOneTest = new ArrayList<Student>();
for(Student student : studentIt) {
List<Test> tests = student.getTests(); /* no shallow copy this time */
if (tests.equals(null) || (tests.size == 0)) {
continue;
} else {
boolean shouldSelectThisStudent = false;
Iterator<Tests> testIt = tests.iterator();
for(Test test : testIt) {
/* many Java programmers would short circuit this -- I don't usually do that.*/
shouldSelectThisStudent = shouldSelectThisStudent || (!test.isTaken());
}
if (shouldSelectThisStudent) {
studentsMissingAtLeastOneTest.addLast(student);
}
}
}
/* now sort by last name */
Comparator<Student> lastNameSorter = new Comparator<Student,Student> {
public int compare(Student student1, Student student2) {
return student1.getLastName().compare(student2.getLastName()); /* dangerous code, please don't template off this, but it's legion in the field */
}
}
return studentsMissingAtLeastOneTest.sort(lastNameSorter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment