Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

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

isStartedBy() is an instance method of the Range class that is used to check whether the range starts with the specified element.

How to import Range

We can find the definition of Range 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 as follows:


import org.apache.commons.lang3.Range;

Syntax


public boolean isStartedBy(final T element)

Parameters

  • final T element: The element to check.

Return value

This method returns true if the specified element starts 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 = 100;
System.out.println(range +" starts by " + element + " - " + range.isStartedBy(element));
// Example 2
element = 99;
System.out.println(range +" starts by " + element + " - " + range.isStartedBy(element));
}
}

Explanation

Example 1

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

The method returns true because the element starts the range.

Example 2

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

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

Output

The output of the code will be as follows:


[100..200] starts by 100 - true
[100..200] starts by 99 - false

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