How can I represent integer intervals in Java?

Tags:



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)?

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)) {
   ....
}


Source: stackoverflow