-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTimeMap2.java
More file actions
31 lines (27 loc) · 803 Bytes
/
TimeMap2.java
File metadata and controls
31 lines (27 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.*;
public class TimeMap2 {
private Map<String,TreeMap<Integer,String>> map;
/** Initialize your data structure here. */
public TimeMap2() {
map = new HashMap<>();
}
public void set(String key, String value, int timestamp) {
if(!map.containsKey(key)) {
map.put(key,new TreeMap<>());
}
map.get(key).put(timestamp,value);
}
public String get(String key, int timestamp) {
TreeMap<Integer,String> treeMap = map.get(key);
if(treeMap==null) {
return "";
}
/** 关键是这个method!! treeMap.floorKey()
* **/
Integer floor = treeMap.floorKey(timestamp);
if(floor==null) {
return "";
}
return treeMap.get(floor);
}
}