Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TimeMap {
- // key -> map of timestamp, value
- Map<String, NavigableMap<Integer, String>> segments;
- public TimeMap() {
- segments = new HashMap<>();
- }
- public void set(String key, String value, int timestamp) {
- NavigableMap<Integer, String> timeMap =
- segments.computeIfAbsent(key, k -> new TreeMap<>());
- timeMap.put(timestamp, value);
- }
- public String get(String key, int timestamp) {
- return Optional.ofNullable(segments.get(key))
- .flatMap(timeMap ->
- Optional.ofNullable(timeMap.floorEntry(timestamp)))
- .map(entry -> entry.getValue())
- .orElse("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement