2011-06-18 9 views
1

フィルタメソッドを提供する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 
    } 
    } 
); 
+0

[Java:コレクションをフィルタリングする最良の方法は何ですか?](http://stackoverflow.com/questions/122105/java-what-is-the-best) -way-to-filter-a-collection) –

答えて

1

機能的な言語に慣れている場合は、フィルタを使用するのが自然な選択です。しかし、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はこのプログラミングスタイルをうまくサポートしていません。 (これは将来変更される可能性があります)

関連する問題