Java中的可过滤集合

| 是否有提供过滤方法的Java集合类?我对Java有点陌生,因此浏览所有集合类以及它们与接口交织的所有微妙方式都有些混乱。我想要的是一个收集类,它执行以下操作:
FilterableCollection<SomeClass> collection = new FilterableCollection<SomeClass>();

// add some elements to the collection

// filter the collection and only keep certain elements
FilterableCollection<SomeClass> filtered_collection = collection.filter(
  new Filter<SomeClass>() {
    @Override
    public boolean test(SomeClass obj) {
      // determine whether obj passes the test or not
    }
  }
);
    
已邀请:
        看看这篇文章。过滤Java集合的最佳方法是什么?它有一个很好的例子,应该正是您要寻找的。     
        如果您习惯使用功能语言,那么使用过滤器是很自然的选择。但是,在Java中,使用循环是一种更简单,更自然和更快的选择。
// filter the collection and only keep certain elements
List<SomeClass> filtered = new ArrayList<SomeClass>();
for(SomeClass sc: collection)
    if(/* determine whether sc passes the test*/)
        filtered.add(sc);
您可以使用函数库,但是在Java中,这些函数库几乎总是会使代码变得更复杂。 Java不能很好地支持这种编程风格。 (将来可能会改变)     

要回复问题请先登录注册