我有Collection<Student>
并想要返回学生列表与过滤器进行比较。
为此我有以下代码
public Collection<Student> findStudents(String filter) {
return // ?
}
我的问题是使用WhereIn/Contains 的返回语句应该是什么?
最佳答案
使用谷歌 Guava :
过滤Collection
学生姓名
public Collection<String> findStudents(String filter) {
Iterable<String> filteredStudents = Iterables.filter(listOfStudentNames, Predicates.containsPattern(filter));
return Lists.newArrayList(filteredStudents);
}
过滤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);
示例:
Iterable<String> filtered = Iterables.filter(Arrays.asList("asdf", "bsdf", "eeadd", "asdfeeee", "123"), Predicates.containsPattern("df"));
filtered
现在包含[asdf, bsdf, asdfeeee]
关于java - 与 Collection 值(value)比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18909440/