Filter and modify list object using java 8 streams

lukassz :

I'am using spring and I defined bean with ArrayList. invites it is a list with Invite objects.

@Getter
public class Invite {

    private String invitee;
    private String email;
    private boolean confirm;
    private String token;
}

This is my data privider class :

@Getter
public class InvitationsData {

    private List<Invite> invites = new ArrayList<>();

    @PostConstruct
    private void initInvites(){
        invites.add(new Invite("John", "[email protected]", false, "6456453"));
        invites.add(new Invite("John", "[email protected]", false, "3252352"));
    }
}

In configuration class I created @Bean from InvitationsData - it works.

In the service I would like to modify one object from list which matches to token string and have set confirm to false.

invitationsData.getInvites()
               .stream()
               .filter(i -> token.equals(i.getToken()))
               .filter(i -> !i.isConfirm())
               .forEach(i -> {
                   i.setConfirm(true);
               });

This stream works fine. Now, when someone call method twice for confirmed object I would like to throw CustomException. How can I do this with this stream? Where can I put orElseThrow?

EDIT:

My current solution. I use peek instead of forEach

invitationsData
                .getInvites()
                .stream()
                .filter(i -> token.equals(i.getToken()))
                .filter(i -> !i.isConfirm())
                .peek(i -> i.setConfirm(true))
                .findFirst()
                .orElseThrow(() -> new InvitationConfirmedException("Error"));
azro :
  • If the token is unique you can do :

    getInvites().stream()
                .filter(i -> token.equals(i.getToken()))
                .filter(i -> !i.isConfirm())
                .findAny()
                .orElseThrow(IllegalArgumentException::new)
                .setConfirm(true);
    
  • If not :

    getInvites().stream()
                .filter(i -> token.equals(i.getToken()))
                .forEach(i -> {
                    if (i.isConfirm()) 
                        throw new CustomException();
                    else 
                        i.setConfirm(true);
                });
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161301&siteId=1