If I have a large Guava Table Like this:
Table<Foo, Range<Long>, Bar> myTable;
And I have a long value, and a Foo object – i want to get the Bar object that has a Range containing my long.
Right now, the best I can do to get all Bar with a Foo key in the Range i need is this:
Map<Range<Long>, Bar> row = cache.row(myFoo);
for (Range<Long> range : row.keySet()) {
if (range.contains(myLong)) {
return Arrays.asList(sample.get(myLong));
}
}
As you can see, i’m grabbing the row from the table, and iterating through the ranges looking for my one Bar with a closed range that contains myLong.
Is there a better way to find my object without the for loop? Getting a RangeMap from that column of Ranges in a way faster than that loop perhaps?
Map<Foo, RangeMap<Long, Bar>>sounds like it’d be perfectly appropriate here. It won’t be quite as nice an API asTable, but it’s the only way to perform that operation in less than linear time.