Data structures that can map a range of keys to a value

后端 未结 7 2004
死守一世寂寞
死守一世寂寞 2020-11-27 18:02

I am trying to find a data structure that takes in a particular value from a range of values and map it to a key.

For example, I have the following conditions:

7条回答
  •  时光取名叫无心
    2020-11-27 18:47

    Guava RangeMap provides specialized solution out of the box:

    RangeMap rangeMap = TreeRangeMap.create();
    rangeMap.put(Range.closed(1, 100), "foo"); // {[1, 100] => "foo"}
    rangeMap.put(Range.open(3, 6), "bar"); // {[1, 3] => "foo", (3, 6) => "bar", [6, 100] => "foo"}
    
    rangeMap.get(42); // returns "foo"
    

提交回复
热议问题