What is Range.fit() in Java?

What is Range.fit() in Java? Answer

Definition

fit() is an instance method of the Range class in Java that fits the given element into the range.

  • If the provided element is within the bounds of the range, the method returns the given element.
  • If the element is out of bounds of the range, then the method returns the closest element in the given range.

How to import the Range class

The definition of the Range class 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>

Please refer to the Maven Repository for other versions of the commons-lang package.

You can import the Range class as follows:

import org.apache.commons.lang3.Range;

Syntax

public T fit(final T element)

Parameters

  • final T element: The element to check.

Return value

This method returns the minimum value of the range, the given element, or the maximum value of the range depending 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.fit(%s) = %s", range, element, range.fit(element));
        System.out.println();

        // Example 2
        element = 55;
        System.out.printf("%s.fit(%s) = %s", range, element, range.fit(element));
        System.out.println();

        // Example 3
        element = 300;
        System.out.printf("%s.fit(%s) = %s", range, element, range.fit(element));

    }
}

Example 1

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

The method returns 150 as the element is within the bounds of the range.

Example 2

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

The method returns 100 as the element is out of bounds of the range.

Example 3

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

The method returns 200 as the element is out of bounds of the range.

Output

The output of the code will be as follows:

[100..200].fit(150) = 150
[100..200].fit(55) = 100
[100..200].fit(300) = 200
What is Range.fit() in Java? Review:

In our experience, we suggest you solve this What is Range.fit() in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the What is Range.fit() in Java?

Find on Educative

Conclusion:

I hope this What is Range.fit() in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.