What is Range.isEndedBy() in Java?

Overview

isEndedBy() is an instance method of the Range class that checks whether the range ends with the specified element or not.

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.

We can import the Range class with the following command:


import org.apache.commons.lang3.Range;

Syntax


public boolean isEndedBy(final T element)

Parameters

  • final T element: The element to check.

Return value

This method returns true if the specified element ends the range, else false.

Code

import org.apache.commons.lang3.Range;
public class Main{
public static void main(String[] args) {
// Example 1
int fromValue = 100;
int toValue = 200;
Range<Integer> range = Range.between(fromValue, toValue);
int element = 200;
System.out.println(range +" ends by " + element + " - " + range.isEndedBy(element));
// Example 2
element = 199;
System.out.println(range +" ends by " + element + " - " + range.isEndedBy(element));
}
}

Example 1

  • range = [100..200]
  • element = 200

The method returns true because the element ends the range.

Example 2

  • range = [100..200]
  • element = 199

The method returns false because the element does not end the range.

Output

The output of the code will be as follows:


[100..200] ends by 200 - true
[100..200] ends by 199 - false

Free Resources