0

I know the basics of predicates and understand below code copied from this question

Predicate<Integer> isEven = new Predicate<Integer>() {
    @Override public boolean apply(Integer number) {
        return (number % 2) == 0;
    }               
};
Iterable<Integer> evenNumbers = Iterables.filter(numbers, isEven);

But is it possible to get an iterable for the items that did not match the predicate (without changing the predicate code)?

Community
  • 1
  • 1
Aksel Willgert
  • 11,367
  • 5
  • 53
  • 74

2 Answers2

8

Use Predicates#not(Predicate).

Iterable<Integer> oddNumbers = Iterables.filter(numbers, Predicates.not(isEven));
Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

You can try:

removeIf(Iterable, Predicate) -->   Removes all elements satisfying the predicate, using the Iterator.remove() method.  

Iterators.removeIf(Iterator, Predicate)

Can find more information here.

nitishagar
  • 9,038
  • 3
  • 28
  • 40