Open In App

Range Class | Guava | Java

Guava’s Range represents an interval, for example, a < range < b. Here range includes any value between a and b, called endpoints which form the boundary. Any value between the boundary is a contiguous span of values of type Comparable.

Declaration : The declaration for com.google.common.collect.Range<C> class is :



@GwtCompatible
public final class Range<C extends Comparable>
   extends Object
      implements Predicate<C>, Serializable

Each end of the range may be bounded or unbounded. If bounded, there is an associated endpoint value. If not it will be treated as infinity. A range can be further defined as either open or closed based whether the range is exclusive or inclusive of the endpoints.

Below given are some methods provided by Range Class of Guava :


Note : When both endpoints exist, the upper endpoint may not be less than the lower. The endpoints may be equal only if at least one of the bounds is closed :



Some other methods provided by Range Class of Guava are :


Exceptions :


Below given are some examples to understand the implementation in a better way :
Example 1 :




// Java code to show implementation
// of Range class of Guava
import com.google.common.collect.Range;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Taking range (1, 5)
        // Note that here 1 and 5 are not included
        Range<Integer> range = Range.open(1, 5);
  
        // Checking if range contains 1 or not
        System.out.println(range.contains(1));
  
        // Checking if range contains 2 or not
        System.out.println(range.contains(2));
  
        // Checking if range contains 3 or not
        System.out.println(range.contains(3));
  
        // Checking if range contains 4 or not
        System.out.println(range.contains(4));
    }
}

Output :

false
true
true
true

Example 2 :




// Java code to show implementation
// of Range class of Guava
import com.google.common.collect.Range;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Taking range [1, 5]
        // Note that here 1 and 5 are included
        Range<Integer> range = Range.closed(1, 5);
  
        // Checking if range contains 1 or not
        System.out.println(range.contains(1));
  
        // Checking if range contains 2 or not
        System.out.println(range.contains(2));
  
        // Checking if range contains 3 or not
        System.out.println(range.contains(3));
  
        // Checking if range contains 5 or not
        System.out.println(range.contains(5));
    }
}

Output :

true
true
true
true

Example 3 :




// Java code to show implementation
// of Range class of Guava
import com.google.common.collect.Range;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Taking range (2, +inf)
        // Note that numbers less than equal to 2
        // are not included
        Range<Integer> range = Range.greaterThan(2);
  
        // Checking if range contains 1 or not
        System.out.println(range.contains(1));
  
        // Checking if range contains 2 or not
        System.out.println(range.contains(2));
  
        // Checking if range contains 130 or not
        System.out.println(range.contains(130));
  
        // Checking if range contains 500 or not
        System.out.println(range.contains(500));
    }
}

Output :

false
false
true
true

Example 4 :




// Java code to show implementation
// of Range class of Guava
import com.google.common.collect.Range;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Taking range (-inf, 2]
        // Note that only numbers less than equal to 2
        // are included
        Range<Integer> range = Range.atMost(2);
  
        // Checking if range contains 1 or not
        System.out.println(range.contains(1));
  
        // Checking if range contains 2 or not
        System.out.println(range.contains(2));
  
        // Checking if range contains -1 or not
        System.out.println(range.contains(-1));
  
        // Checking if range contains 5 or not
        System.out.println(range.contains(5));
    }
}

Output :

true
true
true
false

Article Tags :