3

Consider a list of int[](in array).

Now I want to check the last element of the integer array be equal to 10. If any of the elments in the array is equal to 10 then I want to return true immediately. Else I want to return false.

This is my method to achieve this.

boolean checkList(List<int[]> attrList, Parent parent)  {


    for (int[] list : attrList)
    {
        if(parent.isAttributeEqualsTo10(list[list.length-1]))
              return false;

    }

    return true;
}

Now How will I achieve this using Java 8 streams since we are iterating a collection.

Manu Joy
  • 1,739
  • 6
  • 18
  • 30

1 Answers1

1

Use anyMatch :

return !attrList.stream().anyMatch (l -> parent.isAttributeEqualsTo10(l[l.length-1]));
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    Isn’t `! … anyMatch(…)` the same as `… `[`noneMatch(…)`](http://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#noneMatch-java.util.function.Predicate-)? – Holger Jul 16 '15 at 09:57