Skip to content
Advertisement

How can I represent integer intervals in Java?

We all know about intervals in mathematics (e.g -4 < x < 3).

How can I represent these mathematic intervals in Java, e.g, the numbers between -4 and 3 (-4, -3, …, 2, 3 etc)?

Advertisement

Answer

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)) {
   ....
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement