LoginSignup
9
9

More than 5 years have passed since last update.

Java 8 新機能 Collection#removeIf

Last updated at Posted at 2013-06-30
  • このバージョンは正式リリース前です。
  • 正式リリースされましたが、関連メソッドに変更はない模様

条件に一致するものを削除することためのクラス+メソッドが追加された

基本
        list =new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11));
        list.removeIf(a->{ return a%3==0;});
        list.forEach(a -> { System.out.printf("consumer1: %5d %n", a);});
or
        Predicate<Integer> predicate2 = a->{ return a % 3 == 0;};
        Predicate<Integer> predicate3 = a->{ return a % 5 == 0;};

        list =new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11));
        list.removeIf(predicate2.or(predicate3));
        list.forEach(a -> { System.out.printf("consumer2: %5d %n", a);});
and
        Predicate<Integer> predicate2 = a->{ return a % 3 == 0;};
        Predicate<Integer> predicate3 = a->{ return a % 5 == 0;};

        list =new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11));
        list.removeIf(predicate2.and(predicate3));
        list.forEach(a -> { System.out.printf("consumer3: %5d %n", a);});

実行結果

consumer1:     1 
consumer1:     2 
consumer1:     4 
consumer1:     5 
consumer1:     7 
consumer1:     8 
consumer1:    10 
consumer1:    11 

consumer2:     1 
consumer2:     2 
consumer2:     4 
consumer2:     7 
consumer2:     8 
consumer2:    11 

consumer3:     1 
consumer3:     2 
consumer3:     3 
consumer3:     4 
consumer3:     5 
consumer3:     6 
consumer3:     7 
consumer3:     8 
consumer3:     9 
consumer3:    10 
consumer3:    11 
9
9
0

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
9
9