Javatpoint Logo
Javatpoint Logo

How to Check Whether an Integer Exists in a Range with Java?

We can check whether an integer exists in a range in Java using conditional statements with the lower and upper bounds of the range.

To check whether an integer exists in a range, we can use the following steps:

  1. Define the range (start and end) values.
  2. Compare the integer to the range start and end values.
  3. If the integer is greater than or equal to the range start value and less than or equal to the range end value, then the integer exists in the range. Otherwise, the integer does not exist in the range.

IntegerRangeChecker.java

Output:

7 is within the range [5, 10]

Range Types:

We will focus on these four bounded range types throughout this section.

  • Closed range - includes its lower and upper bounds.
  • Open range - excludes its lower and upper bounds.
  • Left-open right-closed range - Includes its upper bound and excludes it's lower bound.
  • Left-closed right-open range - Includes its lower bound and excludes it's upper bound.

For example, suppose we want to know whether the integer 20 occurs within these two ranges: R1 = [10, 20), a left-closed right-open range, and R2 = (10, 20], a left-open right-closed range. Since R1 does not contain its upper bound, the integer 20 exists only in R2.

Using the < and <= Operators

IntRangeOperators.java

Output:

true
true
true
true

Using Apache Commons Library

Let's move on to some range classes we can use from third-party libraries. First, we will add the Apache Commons dependency to our project:

Here, we're implementing the same behavior as before, but using the Apache Commons Range class:

IntRangeApacheCommons.java

Output:

true (5 is in [1, 10])
false (15 is outside [1, 10])
false (5 is not in (1, 10))
true (6 is in (1, 10))
false (5 is not in (2, 10))
true (5 is in [1, 9])
false (10 is outside [1, 9])

Using IntStream Class

Another approach to check if an integer exists within a range is by utilizing the IntStream class that is available in Java 8 and later versions. The IntStream class provides various methods, including range() and anyMatch() that allow us to create a range of integers and check if a value matches any element in the range.

Here's an example code snippet that demonstrates the usage of the IntStream class:

CheckRange.java

Output:

The number is within the range.

Conclusion:

Checking whether an integer exists within a range is a common task in Java programming. We have explored three different approaches to accomplish this task: using comparison operators, utilizing the Range class from Apache Commons, and leveraging the IntStream class. Each approach has its advantages and can be chosen based on the complexity of the range and the specific requirements of our project.

By mastering these techniques, we will be able to efficiently handle range checks in our Java programs, ensuring that our code operates within the desired boundaries. So go ahead, experiment with these approaches, and unlock the power of range checking in Java.


Next TopicHttpEntity in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA