I am hesitating what is the suitable data structure to save the following data form:
- an integer represents min number
- an integer represents max number
- a string contains a message
So that if I have a result_number I can –
- check if result_number lies between the min & max number
- and display the corresponding message
So what is the appropriate data structure?
If the number of intervals is small, use a
classwith three members (min,max, andmessage), 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
TreeMapusing their left boundary to compare intervals. With thisTreeMapin hand, you can find an interval quickly by callingfloorEntryon the number that you are trying to locate and checking if the returned interval key overlaps the value that you are looking for.