Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is Range.isAfterRange() 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

isAfterRange() is an instance method of the Range class used to check whether the range on which this method is applied is complete after the specified range as a parameter.

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 boolean isAfterRange(final Range<T> otherRange)

Parameters

final Range<T> otherRange is used to check the range.

Return value

This method returns true if the range object on which this method is applied is after the specified range as a parameter. Otherwise, it returns false.

Code

import org.apache.commons.lang3.Range;
public class Main{
public static void main(String[] args) {
int fromValue = 100;
int toValue = 200;
Range<Integer> range1 = Range.between(fromValue, toValue);
fromValue = 300;
toValue = 400;
Range<Integer> range2 = Range.between(fromValue, toValue);
// Example 1
System.out.printf("%s.isAfterRange(%s) = %s", range1, range2, range1.isAfterRange(range2));
System.out.println();
// Example 2
System.out.printf("%s.isAfterRange(%s) = %s", range2, range1, range2.isAfterRange(range1));
System.out.println();
}
}

Example 1

  • range1 = [100..200]
  • range2 = [300..400]

The method returns false because range1 occurs before range2.

Example 2

  • range1 = [100..200]
  • range2 = [300..400]

The method returns true because range2 occurs after range1.

Output

The output of the code will be as follows:


[100..200].isAfterRange([300..400]) = false
[300..400].isAfterRange([100..200]) = true

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