java - 与 Collection 值(value)比较

标签 java gwt collections where-in

我有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/

相关文章:

java - 如何提高 Graphics2D 文本质量?

java - 在链表的单个索引处存储超过 1 个数据项?

c# - List<T> 上的 BinarySearch 似乎返回了奇怪的结果

java - 通过 for 循环循环的通用集合

java - Hibernate 返回 BigIntegers 而不是 longs

java - 根据Java,这个方法是线程安全的吗

java - 如何在 gwt 中的 formPanel 上添加隐藏数据

php - 如何为通用的、可插入的集合设计过滤器?

java - 如何替换EOT字符

java - GWT I18N - 显示列表中的字符串