LoginSignup
6

More than 5 years have passed since last update.

Listの要素をある条件で絞り込む

Last updated at Posted at 2012-06-05

あるListから、その中の特定の条件を満たす要素だけを含んだListを作りたいという場合はGuavaライブラリのIterables.filterを使うと幸せになれる。

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

...
List<SomeObject> original = ...;
List<SomeObject> filteredList = ImmutableList.copyOf(Iterables.filter(
    original, new Predicate<SomeObject>() {
        @Override
        public boolean apply(SomeObject input) {
            // ここで返り値がtrueになる要素だけが結果に含まれるようになる。
            ...
        }
    }));

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6