質問
私は clojure を学んでおり、例によって言語を学ぶのが大好きです。しかし、私はそれについて考えなくても完全な答えが得られるのは好きではありません。
だから私が欲しいのは、私が必要とするかもしれない機能に関するいくつかのヒントと、おそらく他の手がかりです.
私が受け入れる答えは、これを作成するために必要なビルディング ブロックを与えてくれたものです。
public class IntervalMap<K extends Comparable<K>, V> extends
TreeMap<K, V> {
V defaultValue = null;
public IntervalMap(V defaultValue) {
super();
this.defaultValue = defaultValue;
}
/**
*
* Get the value corresponding to the given key
*
* @param key
* @return The value corresponding to the largest key 'k' so that
* " k is the largest value while being smaller than 'key' "
*/
public V getValue(K key) {
// if it is equal to a key in the map, we can already return the
// result
if (containsKey(key))
return super.get(key);
// Find largest key 'k' so that
// " k is the largest value while being smaller than 'key' "
// highest key
K k = lastKey();
while (k.compareTo(key) != -1) {
k = lowerKey(k);
if (k == null)
return defaultValue;
}
return super.get(k);
}
@Override
public V get(Object key) {
return getValue((K) key);
}
}
更新 このクラスの機能を再作成したい