看起来您想要的不止一个
SortedMap;
你想要一个
NavigableMap!具体来说,您可以使用该
floorKey操作。
这是一个例子:
NavigableMap<Integer,String> map = new TreeMap<Integer, String>(); map.put(0, "Kid"); map.put(11, "Teens"); map.put(20, "Twenties"); map.put(30, "Thirties"); map.put(40, "Forties"); map.put(50, "Senior"); map.put(100, "OMG OMG OMG!"); System.out.println(map.get(map.floorKey(13))); // Teens System.out.println(map.get(map.floorKey(29))); // Twenties System.out.println(map.get(map.floorKey(30))); // Thirties System.out.println(map.floorEntry(42).getValue()); // Forties System.out.println(map.get(map.floorKey(666))); // OMG OMG OMG!
需要注意的是也有
ceilingKey,
lowerKey,
higherKey和也,
…Entry而不是
…Key操作以及它返回
Map.Entry<K,V>而不只是
K。



