rand
rand

Reputation: 323

Converting the some logic using java Stream

I would Like to convert some logic using Java8 Stream. How should we modify the code?

public boolean isBFOrder(final BFReturn pReturnRequest) {
        ArrayList<BFReturnShip> shipGroupList =pReturnRequest.getShipGroupList();      
        Boolean bfOrder = false;
        for(BFReturnShip bfReturnShip : shipGroupList) {
            if(bfReturnShip.getModeOfReturn().equals(TYPE)) {
                bfOrder = true;
            } else {
                return false;
            }
        }
        return bfOrder;
    }

Upvotes: 2

Views: 91

Answers (1)

Andrew
Andrew

Reputation: 49606

return pReturnRequest.getShipGroupList()
                     .stream()
                     .allMatch(i -> i.getModeOfReturn().equals(REFUND_ONLY));

Provided that pReturnRequest.getShipGroupList() is never null.

As @Holger points out, we can improve the piece above by covering the case where the list comes empty.

final List<BFReturnShip> list = pReturnRequest.getShipGroupList();
return !list.isEmpty() && 
    list.stream().allMatch(i -> i.getModeOfReturn().equals(REFUND_ONLY));

Upvotes: 5

Related Questions