I have Collection<Student>
and want to return the list of student compare with a filter.
For this I have following code
public Collection<Student> findStudents(String filter) {
return // ?
}
My question is what should be the return statement using WhereIn/Contains ?
Using Google Guava:
Filter a Collection
of student names
public Collection<String> findStudents(String filter) {
Iterable<String> filteredStudents = Iterables.filter(listOfStudentNames, Predicates.containsPattern(filter));
return Lists.newArrayList(filteredStudents);
}
Filter a Collection<Student>
public Collection<Student> findStudents(String filter) {
Iterable<Student> filteredStudents = Iterables.filter(listOfStudents, new Predicate<Student>() {
@Override
public boolean apply(Student student) {
return student.getName().contains(filter);
}
}
}
return Lists.newArrayList(filteredStudents);
Example:
Iterable<String> filtered = Iterables.filter(Arrays.asList("asdf", "bsdf", "eeadd", "asdfeeee", "123"), Predicates.containsPattern("df"));
filtered
now contains [asdf, bsdf, asdfeeee]