Suppose I have an unsorted array of ranges. For example,
class CreditRange{ long credits; int id; }
Now I want to find if the value of the credit account belongs to one of CreditRange.
Possible values ββfor Set<CreditRange> may be
CreditRange :{id:1,credits:0} CreditRange :{id:2,credits:100} CreditRange :{id:3,credits:500} CreditRange :{id:4,credits:250}
Case 1: now when the user enters Credits = 50, this range comparator should give answer as
CreditRange :{id:1,credits:0}
Case 2: now, when the user enters Credits = 300, this range comparator should give answer as
CreditRange :{id:4,credits:250}
Case 3: now, when the user enters Credits = 600, this range comparator should give answer as
CreditRange :{id:3,credits:500}
We can assume that the range array accepts ~ 1M and is suitable for memory. I am looking for a simple algorithm that uses only standard JDK collections without any 3d party libraries and special data structures, but works fast enough.
What would you suggest?
java collections
Yashpal singla
source share