import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
class MapSum {
private Map<String, Integer> map = new HashMap<>();
/**
* Initialize your data structure here.
*/
public MapSum() {
}
public void insert(String key, int val) {
map.put(key, val);
}
public int sum(String prefix) {
int sum = 0;
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> next = iterator.next();
if (next.getKey().startsWith(prefix)) {
sum += next.getValue();
}
}
return sum;
}
}
/**
* Your MapSum object will be instantiated and called as such:
* MapSum obj = new MapSum();
* obj.insert(key,val);
* int param_2 = obj.sum(prefix);
*/
图艺博知识网