Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TimeMap {
- Map<String,Map<Integer,String>> map;
- /** Initialize your data structure here. */
- public TimeMap( ) {
- this.map= new HashMap<>();
- }
- public void set(String key, String value, int timestamp) {
- map.putIfAbsent(key,new HashMap<Integer,String>());
- Map<Integer,String> internalMap= map.get(key);
- internalMap.put(timestamp,value);
- }
- public String get(String key, int timestamp) {
- if(!map.containsKey(key)){
- return "";
- }
- Map<Integer,String> internalMap = map.get(key);
- while(timestamp > 0 && !internalMap.containsKey(timestamp)){
- timestamp--;
- }
- if(timestamp==0){
- return "";
- }else{
- return internalMap.get(timestamp);
- }
- }
- }
- /**
- * Your TimeMap object will be instantiated and called as such:
- * TimeMap obj = new TimeMap();
- * obj.set(key,value,timestamp);
- * String param_2 = obj.get(key,timestamp);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement