How can I represent integer intervals in Java?
Check apache commons-lang IntRange
. So, if you want to check if a number is in a given interval (range), you do:
IntRange range = new IntRange(-4, 3);
if (range.contains(x)) {
....
}
You simply have to separate -4 < x < 3
into -4 < x
and x < 3
, like this:
if (-4 < x && x < 3)
{
. . .
}
Google Guava also has a Range class (https://guava.dev/releases/19.0/api/docs/com/google/common/collect/Range.html) that may work for you.