forked from christine-1017/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapWithTimestamp.java
More file actions
72 lines (64 loc) · 1.99 KB
/
HashMapWithTimestamp.java
File metadata and controls
72 lines (64 loc) · 1.99 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
* HashMap with Timestamp.
*
* get(key, timestamp), if timestamp is given, return the value with largest time <= timestamp.
* get(key) if timestamp is not given, return the lastest value.
*
*
* put(key, timestamp, value)
*
* @param <K> type of key
* @param <V> type of value
*/
public class HashMapWithTimestamp<K, V> {
private Map<K, TreeMap<Integer, V>> map = new HashMap<>();
public V get(final K k, final Integer time) {
TreeMap<Integer, V> treeMap = map.get(k);
if (treeMap == null) {
return null;
} else {
V val = treeMap.get(time);
if (val != null) {
return val;
} else {
return treeMap.lowerEntry(time).getValue();
}
}
}
public V get(final K k) {
TreeMap<Integer, V> treeMap = map.get(k);
if (treeMap == null) {
return null;
} else {
Entry<Integer, V> entry = treeMap.lastEntry();
if (entry != null) {
return entry.getValue();
} else {
return null;
}
}
}
public void put(final K k, final V value, final Integer time) {
TreeMap<Integer, V> treeMap = map.get(k);
if (treeMap == null) {
treeMap = new TreeMap<>();
}
treeMap.put(time, value);
map.put(k, treeMap);
}
public static void main(String[] args) {
HashMapWithTimestamp<String, String> map = new HashMapWithTimestamp<>();
map.put("k1", "v1", 10);
System.out.println(map.get("k1")); //v1
System.out.println(map.get("k1", 11)); //v1
map.put("k1", "v2", 20);
System.out.println(map.get("k1", 15)); //v1
System.out.println(map.get("k1", 11)); //v1
System.out.println(map.get("k1", 21)); //v2
System.out.println(map.get("k1")); //v2
}
}