public class ST<Key extends Comparable<Key>, Value> {
private TreeMap<Key, Value> st;
private Function<Key, Value> func;
public ST(Function<Key, Value> func) {
this.st = new TreeMap<>();
this.func = func;
}
public Value get(Key key) {
if (key == null) {
throw new IllegalArgumentException("calls get() with null key");
}
if (st.containsKey(key)) {
return st.get(key);
}
Value value = func.apply(key);
st.put(key, value);
return value;
}
public Value delete(Key key) {
if (key == null) {
throw new IllegalArgumentException("calls delete() with null key");
}
return st.remove(key);
}
public int size() {
return st.size();
}
public Iterable<Key> keys() {
return st.keySet();
}
}