What is Range.is() method in Java?

Overview

The is() is a staticthe methods in Java that can be called without creating an object of the class. method of the Range class in Java - which is used to obtain a Range object using the specified element as both the minimum and maximum in the range.

The class of the element has to implement the Comparable interface.

How to import Range

The definition of Range can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the Range class as follows:


import org.apache.commons.lang3.Range;

Syntax


public static <T extends Comparable<T>> Range<T> is(final T element)

Parameters

  • final T element: The element to use as the maximum and minimum value.

Return value

This method returns a Range object.

Code

import org.apache.commons.lang3.Range;
public class Main{
public static void main(String[] args) {
int element = 100;
Range<Integer> range = Range.is(element);
System.out.printf("Range object - %s\n", range);
}
}

Explanation

  • In line 3, we define the Main class.
  • In line 5, we define the main method.
  • In line 6, we define the element to be used as the maximum and minimum value for the Range object.
  • In line 7, we obtain a Range object using the is() method.
  • In line 8, we print the object to the console.

Output

The output of the code will be as follows:


Range object - [100..100]

Free Resources