public static void main(String[] args) { TreeMap tm = new TreeMap(); tm.put(new R(3), "轻量级Java EE企业应用实战"); tm.put(new R(-5), "疯狂Java讲义"); tm.put(new R(9), "疯狂Android讲义"); System.out.println(tm); // 返回该TreeMap的第一个Entry对象 System.out.println(tm.firstEntry()); // 返回该TreeMap的最后一个key值 System.out.println(tm.lastKey()); // 返回该TreeMap的比new R(2)大的最小key值。 System.out.println(tm.higherKey(new R(2))); // 返回该TreeMap的比new R(2)小的最大的key-value对。 System.out.println(tm.lowerEntry(new R(2))); // 返回该TreeMap的子TreeMap System.out.println(tm.subMap(new R(-1), new R(4))); }
public double getValue(double x) { Map.Entry<Double, Double> e1, e2; double x1, x2; double y1, y2; if (Double.isNaN(x)) return Double.NaN; e1 = sortMap.floorEntry(x); if (e1 == null) { // x smaller than any value in the set e1 = sortMap.firstEntry(); if (e1 == null) { return Double.NaN; } e2 = sortMap.higherEntry(e1.getKey()); if (e2 == null) { // only one value in the set return e1.getValue(); } } else { e2 = sortMap.higherEntry(e1.getKey()); if (e2 == null) { // x larger than any value in the set e2 = e1; e1 = sortMap.lowerEntry(e2.getKey()); if (e1 == null) { // only one value in the set return e2.getValue(); } } } x1 = e1.getKey(); x2 = e2.getKey(); y1 = e1.getValue(); y2 = e2.getValue(); return (x - x1) / (x2 - x1) * (y2 - y1) + y1; }
public Map.Entry<K, V> lowerEntry(K key) { return realMap.lowerEntry(key); }
/** Return the endpoint that has the highest token less than this ep's token */ EndPoint getTokenBefore(EndPoint ep) { BigInteger token = endPointToTokenMap_.get(ep); Map.Entry<BigInteger, EndPoint> entry = tokenToEndPointMap_.lowerEntry(token); if (entry != null) return entry.getValue(); return tokenToEndPointMap_.lastEntry().getValue(); // wrap }