eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Overview

boolean is one of Java’s primitives. It’s a pretty straightforward data type with only two values: true and false.

In this tutorial, we’ll look into a problem: checking if there are at least two true in the given three booleans.

2. Introduction to the Problem

The problem is pretty straightforward. We’ll be given three booleans. If at least two of them are true, our method should return true.

Solving the problem isn’t a challenge for us. However, in this tutorial, we’ll explore a few nice solutions. Further, we’ll discuss if each approach can be easily extended to solve a general problem: given n booleans, check if at least x from them are true.

We’ll verify each approach by unit tests. Therefore, let’s first create a Map object to hold test cases and the expected results:

static final Map<boolean[], Boolean> TEST_CASES_AND_EXPECTED = ImmutableMap.of(
    new boolean[]{true, true, true}, true,
    new boolean[]{true, true, false}, true,
    new boolean[]{true, false, false}, false,
    new boolean[]{false, false, false}, false
);

As the code above shows, the TEST_CASES_AND_EXPECTED map carries four scenarios and their expected results. Later, we’ll go through this map object and pass each boolean array as the parameter to each approach and verify if the method returns the expected value.

Next, let’s see how to solve the problem.

3. Looping Through the Three Booleans

The most straightforward idea to solve the problem could be walking through the three given booleans and counting trues.

We stop the checking and return true once the counter is greater than or equal to 2. Otherwise, the number of trues in the three booleans is less than 2. Thus, we return false:

public static boolean twoOrMoreAreTrueByLoop(boolean a, boolean b, boolean c) {
    int count = 0;
    for (boolean i : new Boolean[] { a, b, c }) {
        count += i ? 1 : 0;
        if (count >= 2) {
            return true;
        }
    }
    return false;
}

Next, let’s use our TEST_CASES_AND_EXPECTED map to test if this method works:

TEST_CASES_AND_EXPECTED.forEach((array, expected) -> 
  assertThat(ThreeBooleans.twoOrMoreAreTrueByLoop(array[0], array[1], array[2])).isEqualTo(expected));

If we run this test, unsurprisingly, it passes.

This approach is pretty easy to understand. Further, suppose we change the method’s argument to a boolean array (or a Collection) and an int x. In that case, it can be easily extended to become a generic solution to solving the problem: given n booleans, check if at least x of them are true:

public static boolean xOrMoreAreTrueByLoop(boolean[] booleans, int x) {
    int count = 0;
    for (boolean i : booleans) { 
        count += i ? 1 : 0;
        if (count >= x) {
            return true;
        }
    }
    return false;
}

4. Converting Booleans Into Numbers

Similarly, we can convert the three booleans into numbers and calculate their sum and check if it’s 2 or greater:

public static boolean twoOrMoreAreTrueBySum(boolean a, boolean b, boolean c) {
    return (a ? 1 : 0) + (b ? 1 : 0) + (c ? 1 : 0) >= 2;
}

Let’s execute the test to make sure it works as expected:

TEST_CASES_AND_EXPECTED.forEach((array, expected) -> 
  assertThat(ThreeBooleans.twoOrMoreAreTrueBySum(array[0], array[1], array[2])).isEqualTo(expected));

We can also turn this approach into a general solution to check at least x trues from n booleans:

public static boolean xOrMoreAreTrueBySum(Boolean[] booleans, int x) {
    return Arrays.stream(booleans)
      .mapToInt(b -> Boolean.TRUE.equals(b) ? 1 : 0)
      .sum() >= x;
}

We’ve used the Stream API to convert each boolean into an int and calculate the sum in the code above.

5. Using Logical Operators

We’ve solved the problem by converting booleans into integers. Alternatively, we can use logical operations to determine if at least two of three booleans are true.

We can perform the logical AND (&&) operation on every two booleans. So, we’ll do three AND operations on the given three booleans. If two of three booleans are true, then at least one logical AND operation should result in true:

public static boolean twoOrMoreAreTrueByOpeators(boolean a, boolean b, boolean c) {
    return (a && b) || (a && c) || (b && c);
}

Next, if we test this method using the TEST_CASES_AND_EXPECTED map, it passes, too:

TEST_CASES_AND_EXPECTED.forEach((array, expected) -> 
  assertThat(ThreeBooleans.twoOrMoreAreTrueByOpeators(array[0], array[1], array[2])).isEqualTo(expected));

Now, let’s think about if we can extend this approach to the general case. It works only when x is 2. Also, if the n is large enough, we may need to build a long logical operation chain.

Therefore, it’s not suitable for a general problem.

6. Using the Karnaugh Map

Karnaugh Map is a method of simplifying boolean algebra expressions. Also, we can write the expression from a Karnaugh Map. Therefore, sometimes, it can help us solve complex boolean algebra problems.

Next, let’s see how to solve this problem using the Karnaugh Map. Given that we have three booleans, A, B, and C, we can build a Karnaugh Map:

      | C | !C
------|---|----
 A  B | 1 | 1 
 A !B | 1 | 0
!A !B | 0 | 0
!A  B | 1 | 0

In the table above, A, B, and C indicate their true values. Oppositely, !A, !B, and !C mean their false values.

So, as we’ve seen, the table covers all possible combinations of the given three booleans. Moreover, we can find all combination cases in which at least two booleans are true. For these cases, we’ve written ‘1’ to the table. Therefore, there are two groups containing the ones: the first row (group 1) and the first column (group 2).

Thus, the final boolean algebra expression to produce one would be: (the expression to obtain all ones in group1 ) || (the expression to obtain all ones in group2)

Next, let’s divide and conquer.

  • Group 1 (the first row) – A and B are both true. No matter what value C has, we’ll have one. Therefore, we have: A && B
  • Group 2 (the first column) – First, C is always true. Moreover, there must be at least one true in A and B. Therefore, we get: C && (A || B)

Finally, let’s combine the two groups and get the solution:

public static boolean twoorMoreAreTrueByKarnaughMap(boolean a, boolean b, boolean c) {
    return (c && (a || b)) || (a && b);
}

Now, let’s test if the method works as expected:

TEST_CASES_AND_EXPECTED.forEach((array, expected) -> 
  assertThat(ThreeBooleans.twoorMoreAreTrueByKarnaughMap(array[0], array[1], array[2])).isEqualTo(expected));

If we execute the test, it passes. That is to say, the method does the job.

However, if we try to use this method to solve the general problem, it could be a difficult job to produce the table when n is large.

Therefore, even though the Karnaugh Map is good at solving complex boolean algebra problems, it’s not suitable for some dynamic and general tasks.

7. Using the xor Operator

Finally, let’s have a look at another interesting approach.

In this problem, we’re given three booleans. Further, we’ve known a boolean can only have two different values: true and false.

So, let’s first take any two booleans from the three, say a and b. Then, we check the result of the expression a != b:

  • a != b is true – either a or b is true. So, if c is true, then we have two trues. Otherwise, we have two false in the three booleans. That is to say, c‘s value is the answer.
  • a != b is falsea and b have the same value. Since we have only three booleans, a (or b) value is the answer.

Therefore, we can conclude the solution: a != b ? c : a. Moreover, the a != b check is actually an XOR operation. Hence, the solution can be as simple as:

public static boolean twoOrMoreAreTrueByXor(boolean a, boolean b, boolean c) {
    return a ^ b ? c : a;
}

When we test the method using the TEST_CASES_AND_EXPECTED map, the test will pass:

TEST_CASES_AND_EXPECTED.forEach((array, expected) -> 
  assertThat(ThreeBooleans.twoOrMoreAreTrueByXor(array[0], array[1], array[2])).isEqualTo(expected));

This solution is pretty compact and tricky. However, we cannot extend it to solve the general problem.

8. Conclusion

In this article, we’ve explored a few different approaches to check if there are at least two trues in three given booleans.

Moreover, we’ve discussed which approach can be easily extended to solve a more general problem: check if there are at least x trues in n booleans.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)