How to search within list of integer ranges efficiently?
I have list of ranges with some duplicate values. I want to get value, if input number is within range.
e.g.
| Range Start | Range End | Value |
|---|---|---|
| 10 | 75 | A |
| 95 | 200 | A |
| 300 | 455 | B |
| 570 | 650 | C |
| 201 | 250 | A |
| 255 | 275 | B |
Note: Start and end ranges are not overlapped.
- Input 260 Expected output B
- Input 195 Expected output A
Currently, I am saving in HashMap <String, String> and Storing {“10-75” , A} {’95-200”, B}… I am
- looping through all keys of Map,
- split each key,
- convert to int and
- check given number is within range or not.
- if yes get value for that key
I am thinking there may be some more efficient way to handle this in Java.
Any help would be greatly appreciated.