Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the Range.elementCompareTo() method in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The elementCompareTo() method is an instance method of the Range that checks the occurrence of the specified element relative to the range. The method returns:

  • -1 if the element occurs before the range.
  • 0 if the element occurs within the range.
  • 1 if the element occurs after the range.

How to import

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 int elementCompareTo(final T element)

Parameters

  • final T element: The element to check.

Return value

This method returns +1, 0, or -1 based on the element’s location relative to the range.

Code

import org.apache.commons.lang3.Range;
public class Main{
public static void main(String[] args) {
int fromValue = 100;
int toValue = 200;
Range<Integer> range = Range.between(fromValue, toValue);
// Example 1
int element = 150;
System.out.printf("%s.elementCompareTo(%s) = %s", range, element, range.elementCompareTo(element));
System.out.println();
// Example 2
element = 55;
System.out.printf("%s.elementCompareTo(%s) = %s", range, element, range.elementCompareTo(element));
System.out.println();
// Example 3
element = 300;
System.out.printf("%s.elementCompareTo(%s) = %s", range, element, range.elementCompareTo(element));
}
}
Implementation of Range.elementCompareTo() method in Java

Example 1

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

The method returns 0 as the element occurs within the range.

Example 2

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

The method returns -1 as the element occurs before the range.

Example 3

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

The method returns 1 as the element occurs after the range.

Output

The output of the code will be as follows:


[100..200].elementCompareTo(150) = 0
[100..200].elementCompareTo(55) = -1
[100..200].elementCompareTo(300) = 1

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring