Java 8 : How to stop a foreach process when you are using Streams ?

For do this you can use :


The anyMatch will make the interaction until take a conditional return equals true. I will put here a good example.

Full Example:

private Map<MyObject, List<MySecondObject>> myMap;
myMap.entrySet().stream().forEach( m -> {
    m.getValue().stream().forEach( value-> {
        value.getComponent().stream().anyMatch(c -> {
            if (c.getId().equals(myEspecialId)) {
                if (c.getName() == null) {
                    return true;
                } else {
                    return true;
            return false;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s