您的范围不重叠吗?如果是这样,您可以使用TreeMap:
TreeMap<Double, Character> m = new TreeMap<Double, Character>();m.put(1.0, 'A');m.put(2.9, null);m.put(4.0, 'B');m.put(6.0, null);m.put(6.5, 'C');m.put(10.0, null);
由于您可能想要包含性查找(例如,2.9映射为“ A”而不是未定义),因此查找逻辑有些复杂:
private static <K, V> V mappedValue(TreeMap<K, V> map, K key) { Entry<K, V> e = map.floorEntry(key); if (e != null && e.getValue() == null) { e = map.lowerEntry(key); } return e == null ? null : e.getValue();}例:
mappedValue(m, 5) == 'B'
更多结果包括:
0.9 null1.0 A1.1 A2.8 A2.9 A3.0 null6.4 null6.5 C6.6 C9.9 C10.0 C10.1 null



