You can write, given for instance a List<Boolean>:

if (!list.stream().allMatch(x -> x)) {
    // not every member is true
}

Or:

if (list.stream().anyMatch(x -> !x)) {
    // at least one member is false
}

If you have an array of booleans, then use Arrays.stream() to obtain a stream out of it instead.


More generally, for a Stream providing elements of (generic) type X, you have to provide a Predicate<? super X> to .{all,any}Match() (either a "full" predicate, or a lambda, or a method reference -- many things go). The return value of these methods are self explanatory -- I think.


Now, to count elements which obey a certain predicate, you have .count(), which you can combine with .filter() -- which also takes (whatever is) a Predicate as an argument. For instance checking if you have more than 2 elements in a List<String> whose length is greater than 5 you'd do:

if (list.stream().filter(s -> s.length() > 5).count() > 2L) {
    // Yup...
}
Answer from fge on Stack Overflow
Top answer
1 of 2
13

You can write, given for instance a List<Boolean>:

if (!list.stream().allMatch(x -> x)) {
    // not every member is true
}

Or:

if (list.stream().anyMatch(x -> !x)) {
    // at least one member is false
}

If you have an array of booleans, then use Arrays.stream() to obtain a stream out of it instead.


More generally, for a Stream providing elements of (generic) type X, you have to provide a Predicate<? super X> to .{all,any}Match() (either a "full" predicate, or a lambda, or a method reference -- many things go). The return value of these methods are self explanatory -- I think.


Now, to count elements which obey a certain predicate, you have .count(), which you can combine with .filter() -- which also takes (whatever is) a Predicate as an argument. For instance checking if you have more than 2 elements in a List<String> whose length is greater than 5 you'd do:

if (list.stream().filter(s -> s.length() > 5).count() > 2L) {
    // Yup...
}
2 of 2
4

Your problem

Your current problem is that you use directly a lambda expression. Lambdas are instances of functional interfaces. Your lambda does not have the boolean type, that's why your if does not accept it.

This special case's solution

You can use a stream from your collections of booleans here.

if (bools.stream().allMatch((Boolean b)->b)) {
    // do something
}

It is actually much more powerful than this, but this does the trick I believe.

General hint

Basically, since you want an if condition, you want a boolean result. Since your result depends on a collection, you can use Java 8 streams on collections.

Java 8 streams allow you to do many operations on a collection, and finish with a terminal operation. You can do whatever complicated stuff you want with Stream's non-terminal operations. In the end you need one of 2 things:

  • use a terminal operation that returns a boolean (such as allMatch, anyMatch...), and you're done
  • use any terminal operation, but use it in a boolean expression, such as myStream.filter(...).limit(...).count() > 2

You should have a look at your possibilities in this Stream documentation or this one.

🌐
W3Schools
w3schools.com › java › java_lambda.asp
Java Lambda Expressions
The simplest lambda expression contains a single parameter and an expression: ... Simple expressions must return a value immediately. They cannot contain multiple statements, such as loops or if conditions...
🌐
Javatpoint
javatpoint.com › if-condition-in-lambda-expression-java
if Condition in Lambda Expression Java - Javatpoint
if Condition in Lambda Expression Java with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
TutorialsPoint
tutorialspoint.com › how-to-write-a-conditional-expression-in-lambda-expression-in-java
How to write a conditional expression in lambda expression in Java?
interface Algebra { int substraction(int a, int b); } public class ConditionalExpressionLambdaTest { public static void main(String args[]) { System.out.println("The value is: " + getAlgebra(false).substraction(20, 40)); System.out.println("The value is: " + getAlgebra(true).substraction(40, 10)); } static Algebra getAlgebra(boolean reverse) { Algebra alg = reverse ? (a, b) -> a - b : (a, b) -> b - a; // conditional expression return alg; } }
Top answer
1 of 5
39

As it almost but not really matches Optional, maybe you might reconsider the logic:

Java 8 has a limited expressiveness:

Optional<Elem> element = ...
element.ifPresent(el -> System.out.println("Present " + el);
System.out.println(element.orElse(DEFAULT_ELEM));

Here the map might restrict the view on the element:

element.map(el -> el.mySpecialView()).ifPresent(System.out::println);

Java 9:

element.ifPresentOrElse(el -> System.out.println("Present " + el,
                        () -> System.out.println("Not present"));

In general the two branches are asymmetric.

2 of 5
21

It's called a 'fluent interface'. Simply change the return type and return this; to allow you to chain the methods:

public MyClass ifExist(Consumer<Element> consumer) {
    if (exist()) {
        consumer.accept(this);
    }
    return this;
}

public MyClass ifNotExist(Consumer<Element> consumer) {
    if (!exist()) {
        consumer.accept(this);
    }
    return this;
}

You could get a bit fancier and return an intermediate type:

interface Else<T>
{
    public void otherwise(Consumer<T> consumer); // 'else' is a keyword
}

class DefaultElse<T> implements Else<T>
{
    private final T item;

    DefaultElse(final T item) { this.item = item; }

    public void otherwise(Consumer<T> consumer)
    {
        consumer.accept(item);
    }
}

class NoopElse<T> implements Else<T>
{
    public void otherwise(Consumer<T> consumer) { }
}

public Else<MyClass> ifExist(Consumer<Element> consumer) {
    if (exist()) {
        consumer.accept(this);
        return new NoopElse<>();
    }
    return new DefaultElse<>(this);
}

Sample usage:

element.ifExist(el -> {
    //do something
})
.otherwise(el -> {
    //do something else
});
🌐
W3Schools
w3schools.com › java › java_conditions.asp
Java Conditions and If Statements
The condition inside the if statement must result in a boolean value - it can be either a boolean expression (like x > y) or a boolean variable (like isLightOn).
🌐
GeeksforGeeks
geeksforgeeks.org › java › lambda-expressions-java-8
Java Lambda Expressions - GeeksforGeeks
This is a zero-parameter lambda expression! ... It is not mandatory to use parentheses if the type of that variable can be inferred from the context. Parentheses are optional if the compiler can infer the parameter type from the functional interface. ... import java.util.ArrayList; public class GFG{ public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); System.out.println("All elements:"); list.forEach(n -> System.out.println(n)); System.out.println("Even elements:"); list.forEach(n -> { if (n % 2 == 0) System.out.println(n); }); } }
Published   3 weeks ago
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › using ‘if-else’ conditions with java streams
Using 'if-else' Conditions with Java Streams - HowToDoInJava
March 3, 2022 - Learn to use the if-else conditions logic using Java Stream API to filter the items from a collection based on certain conditions. The 'if-else' condition can be applied as a lambda expression in forEach() function in form of a Consumer action.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 45006435 › how-to-make-lambda-expression-as-a-condition-in-if-then-statement
java - How to make lambda expression as a condition in if-then statement - Stack Overflow
public static void main(String[] args) { boolean ifResult = lambdaIf( (pInt) -> { //Call lambdaIf with a function if(pInt == 0) { //Our lambda has an if-case for its input return true; } else { return false; } } ); System.out.println(ifResult); } ...
🌐
Code with Mosh
forum.codewithmosh.com › t › using-lambda-expressions-for-if-statements › 13119
Using lambda expressions for if statements - Code with Mosh Forum
June 16, 2022 - Hello all! First post here haha, I’ll get right to it :slight_smile: I was recently asked as a challenge to represent a validation method as a lambda expression and the implication was that this was somehow more effic…
🌐
Tpoint Tech
tpointtech.com › if-condition-in-lambda-expression-java
if Condition in Lambda Expression Java - Tpoint Tech
Lambda expressions are one of the most powerful features introduced in Java 8. They are a concise way to express functions and can make your code much more r...
Top answer
1 of 3
6

As a stream call chain is complex make two streams - avoiding the conditional branches.

String ncourseIds = equivalentCourses.stream()
   .filter(equivalentCourse -> equivalentCourse.getNcourse() != null)
   .map(EquivalentCourse::getNcourse)
   .map(x -> String.valueOf(x.getId()))
   .collect(Collectors.joining(", "));

String pastCourseIds = equivalentCourses.stream()
   .filter(equivalentCourse -> equivalentCourse.getNcourse() == null
          && equivalentCourse.getPastCourse() != null)
   .map(EquivalentCourse::getPastCourse)
   .map(x -> String.valueOf(x.getId()))
   .collect(Collectors.joining(", "));

This also is code focusing on the resulting two strings, with an efficient joining.

By the way, if this is for an SQL string, you may use a PreparedStatement with an Array.


Embellishment as commented by @Holger:

String ncourseIds = equivalentCourses.stream()
   .map(EquivalentCourse::getNcourse)
   .filter(Objects::nonNull)
   .map(NCourse::getId)
   .map(String::valueOf)
   .collect(Collectors.joining(", "));

String pastCourseIds = equivalentCourses.stream()
   .filter(equivalentCourse -> equivalentCourse.getNcourse() == null)
   .map(EquivalentCourse::getPastCourse)
   .filter(Objects::nonNull)
   .map(EquivalentCourse::getPastCourse)
   .map(PastCourse::getId)
   .map(String::valueOf)
   .collect(Collectors.joining(", "));
2 of 3
1

You could group by condition and then remap:

public void booleanGrouping() throws Exception {
    List<String> strings = new ArrayList<>();
    strings.add("ala");
    strings.add("ela");
    strings.add("jan");

    strings.stream()
            .collect(
                    Collectors.groupingBy(s -> s.endsWith("a")) // using function Obj -> Bool not predicate
            ).entrySet()
            .stream()
            .collect(
                    Collectors.toMap(
                            e -> e.getKey() ? "Present" : "Past",
                            e -> e.getValue().stream().collect(Collectors.joining(""))
                    )
            );
}

First stream group by condition, you should use equivalentCourse.getNcourse() != null second remap collections from value to string. You could introduce:

enum PresentPast{
    Present, Past
    PresentPast is(boolean v){
         return v ? Present : Past
    }
}

and change e -> e.getKey() ? "Present" : "Past" to enum based solution.

Edit:

Solution for else if:

public Map<Classifier, String> booleanGrouping() throws Exception {
    List<String> strings = new ArrayList<>();
    strings.add("ala");
    strings.add("ela");
    strings.add("jan");
    // our ifs:
    /*
        if(!string.endsWith("n")){
        }else if(string.startsWith("e")){}

        final map should contains two elements
        endsWithN -> ["jan"]
        startsWithE -> ["ela"]
        NOT_MATCH -> ["ala"]

     */
    return strings.stream()
            .collect(
                    Collectors.groupingBy(Classifier::apply) // using function Obj -> Bool not predicate
            ).entrySet()
            .stream()
            .collect(
                    Collectors.toMap(
                            e -> e.getKey(),
                            e -> e.getValue().stream().collect(Collectors.joining(""))
                    )
            );
}

enum Classifier implements Predicate<String> {
    ENDS_WITH_N {
        @Override
        public boolean test(String s) {
            return s.endsWith("n");
        }
    },
    STARTS_WITH_E {
        @Override
        public boolean test(String s) {
            return s.startsWith("e");
        }
    }, NOT_MATCH {
        @Override
        public boolean test(String s) {
            return false;
        }
    };

    public static Classifier apply(String s) {
        return Arrays.stream(Classifier.values())
                .filter(c -> c.test(s))
                .findFirst().orElse(NOT_MATCH);
    }
}
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › lambdaexpressions.html
Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
If the Person instance does satisfy the criteria specified by tester, the method printPerson is invoked on the Person instance. Instead of invoking the method printPerson, you can specify a different action to perform on those Person instances that satisfy the criteria specified by tester. You can specify this action with a lambda expression.
Top answer
1 of 2
2

If/else or even switch are not the "good OO design" answer. Neither are lambdas. Retrieving the status from somewhere, to make then a decision based on that - that is procedural programming; not OO.

You see, what you actually have in front of you - is a state machine. You have different states; and the point is: if your "thing" is in state A; then you want to do something ( invoke handleA() ). If your are in state B; you want to do something too ( like invoke handleB() ).

So, what you actually have is:

abstract class State {
  abstract void doIt();
...

StateA extends State {
  @Override
  void doIt() { handleA(); }

So, from the client side, you just call doIt() on same object of class State.

Thus: if you are really interested in improving your code base, learn how to use polymorphism to get rid of your if/else/switch statements.

You can watch this video to get an idea what I am talking about.

2 of 2
0

Maybe I hadn't fully explained my problem, but my colleague proposed another solution that I really like. He suggested I make the if-else block a java.util.Supplier, and invoke it in the Entity class where I need it.

So my code went from this block of logic sprinkled everywhere:

public class ServiceImpl {
  ...
  if (entity.getStatus) == 'A' then
    finalStatus = handleA();
  else if (entity.getStatus() == 'B') then
    finalStatus = handleB();
  else
    finalStatus = handleEverythingElse();
  ...
}

To this nicely compacted form:

public class ServiceImpl {
  finalStatus = entity.getFinalStatus(this::handleStatus);

  public int handleStatus() {
    return dao.getStatus();
  }
}

With the implementation in my Entity class:

public class Entity {
  public int handleStatus(Supplier<Integer> s) {
    int finalStatus;
    if (status) == 'A' then
      finalStatus = handleA();
    else if (status() == 'B') then
      finalStatus = handleB();
    else
      finalStatus = supplier.get();
    return status;
  }
}

I hope this make sense...

🌐
Stack Overflow
stackoverflow.com › questions › 33249601 › java-if-else-in-a-lambda-expression
if statement - Java if else in a lambda expression - Stack Overflow
October 21, 2015 - The type of the given lambda should be a FunctionalInterface with no argument and result Boolean, e.g.
🌐
GeeksforGeeks
geeksforgeeks.org › java › block-lambda-expressions-in-java
Block Lambda Expressions in Java - GeeksforGeeks
July 23, 2025 - // Java Program to illustrate Lambda ... method public static void main(String[] args) { // Lambda expression body If1 isEven = (n) -> (n % 2) == 0; // Above is lambda expression which tests // passed number is even or odd // Condition ...
🌐
W3Schools
w3schoolsua.github.io › java › java_lambda_en.html
Java Lambda Expressions. Lessons for beginners. W3Schools in English
The simplest lambda expression contains a single parameter and an expression: ... Expressions are limited. They have to immediately return a value, and they cannot contain variables, assignments or statements such as if or for.
🌐
Medium
medium.com › @marcelogdomingues › java-lambda-expressions-techniques-for-advanced-developersava-lambda-expressions-techniques-for-c1d71c30bb1f
Java Lambda Expressions: Techniques for Advanced Developersava Lambda Expressions: Techniques for…
June 21, 2024 - If the body is a single expression, the return keyword and curly braces can be omitted. ... Before lambda expressions, implementing functional interfaces required creating anonymous inner classes.