Manu Joy
Manu Joy

Reputation: 1769

return a value immediately from collection by java 8 streams

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.

Upvotes: 3

Views: 293

Answers (1)

Eran
Eran

Reputation: 393781

Use anyMatch :

return !attrList.stream().anyMatch (l -> parent.isAttributeEqualsTo10(l[l.length-1]));

Upvotes: 1

Related Questions