1

I am hesitating what is the suitable data structure to save the following data form:

  1. an integer represents min number
  2. an integer represents max number
  3. a string contains a message

So that if I have a result_number I can -

  1. check if result_number lies between the min & max number
  2. and display the corresponding message

So what is the appropriate data structure?

0

3 Answers 3

7

It sounds like you want...

(Wait for it...)

... a class with two integers and a string.

public final class RangeValidation {
    private final int minimum;
    private final int maximum;
    private final String message;

    public RangeValidation(int minimum, int maximum, String message) {
        if (minimum > maximum) {
            throw new IllegalArgumentException("Invalid min/max combination");
        }
        if (message == null) {
            throw new NullPointerException();
        }
        this.minimum = minimum;
        this.maximum = maximum;
        this.message = message;
    }

    // You can tweak this API, of course...
    // it could throw an exception instead, or return an empty string for
    // success, etc.
    public String validate(int value) {
        return value < minimum || value > maximum ? mesasge : null;
    }
}

Of course you may want to make this implement an interface to fit in with a more general validation framework etc... but the important point is that you've already neatly describe what you want your type to contain - so you just need to write it.

Also note that this counts the maximum value as inclusive - you may wish to make it exclusive, in order to be able to represent empty ranges. (That does make it harder to represent a range for which Integer.MAX_VALUE is valid though...)

Sign up to request clarification or add additional context in comments.

12 Comments

why return null? Why not just return an empty string?
@MyNameIsTooCommon: Null is more of a non-value... I'd expect to check for the return being null to indicate that everything was okay, but checking against an empty string wouldn't be as nice, IMO.
Because an empty string is still a string, and harder to detect from the calling code. Null can never be a string, therefore it cannot be a valid message. Personally, I would rather see validate() return a boolean, and the message can be retrieved for the user when necessary.
@SteveKuo: It's a matter of some debate; I follow Josh Bloch's lead as per Effective Java, 2nd edition. This is also partly out of habit of using Preconditions.checkNotNull, effectively from Guava, at work.
@WaelShowair: Um, what? In order to say that X is more efficient from Y, you've got to have a concrete idea of what X and Y are... just "I want to use collections because they're efficient" isn't a useful approach to problem-solving.
|
3

In java a "data structure" is represented by a class with fields. The class may have methods that use these fields.

Try this:

public class MyClass {

    private int min;
    private int max;
    private String message;

    public void testNumber(int number) {
        if (number >= min && number <= max) {
            System.out.println(message);
        }
    } 
}

You should probably have a constructor to set the values of the fields and I would recommend making the fields final.

Comments

2

If the number of intervals is small, use a class with three members (min, max, and message), store them in a list, and run a linear search.

If the number of intervals is overwhelming, and the timing requirements make linear search prohibitive, create an interval tree, ans associate the messages with its leaf nodes.

If the intervals are not overlapping, you can create an interval class with two ends, and store interval objects in a TreeMap using their left boundary to compare intervals. With this TreeMap in hand, you can find an interval quickly by calling floorEntry on the number that you are trying to locate and checking if the returned interval key overlaps the value that you are looking for.

2 Comments

Thanks for your answer but I was actually looking for a ready-made java data structure like: Maps, Set, hashTable,...etc. Is there any such kind of data structure?
@WaelShowair There is a relatively easy solution for non-overlapping intervals (see my edit) but there is nothing for the general case as far as I know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.