Saturday, 28 September 2013

Find Range in which value lies in Java

Find Range in which value lies in Java

Suppose, I have an unsorted array of ranges. For e.g
class CreditRange{
long credits;
int id;
}
Now I want to find if a given credit belongs to which one of the CreditRange.
Possible Set<CreditRange> of values can be
CreditRange :{id:1,credits:0}
CreditRange :{id:2,credits:100}
CreditRange :{id:3,credits:500}
CreditRange :{id:3,credits:250}
Case 1 : Now when user enters Credits = 50, this range comparator should
give answer as
CreditRange :{id:1,credits:0}
Case 2 : Now when user enters Credits = 300, this range comparator should
give answer as
CreditRange :{id:3,credits:250}
We can assume the ranges array takes ~1M and fits the memory. I am looking
for an easy algorithm, which uses only standard JDK collections without
any 3d-party libraries and special data structures, but works reasonably
fast.
What would you suggest?

No comments:

Post a Comment