Java 用时间概念实现地图
我最近被要求使用以下两种方法实现数据结构: 设置键、值、时间:在指定时间将键设置为值。 getkey,time:获取指定时间或更早时间的key值 如果我们在某个特定的时间设置了一个键,它将永远保持该值,或者直到稍后设置为止 例子 例1 例2 例3Java 用时间概念实现地图,java,performance,dictionary,data-structures,time-complexity,Java,Performance,Dictionary,Data Structures,Time Complexity,我最近被要求使用以下两种方法实现数据结构: 设置键、值、时间:在指定时间将键设置为值。 getkey,time:获取指定时间或更早时间的key值 如果我们在某个特定的时间设置了一个键,它将永远保持该值,或者直到稍后设置为止 例子 例1 例2 例3 以下是我的实现: import java.util.HashMap; import java.util.Map; import java.util.NavigableMap; import java.util.TreeMap; /** * An
以下是我的实现:
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
/**
* An Implementation a Map with the notion of Time.
*
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
* @param <T> the type of time tracked by this map.
* T MUST implement {@link Comparable} to have a notion of comparing times.
* @author Hari Krishnan
* @see Map
* @see NavigableMap
*/
public class TimedMap<K, V, T extends Comparable<T>> {
private final Map<K, NavigableMap<T, V>> map = new HashMap<>();
/**
* Associates the specified value with the specified key at the specified time in this map.
* If the map previously contained a mapping for the key at this time,
* the old value is replaced by the specified value.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @param time time at which said association should occur
*/
public void set(K key, V value, T time) {
if (!map.containsKey(key))
map.put(key, new TreeMap<>());
map.get(key).put(time, value);
}
/**
* Returns the value to which the specified key is mapped at the specified time or earlier,
* or {@code null} if this map contains no mapping for the key at the specified time or earlier.
*
* @param key the key whose associated value is to be returned
* @param time the time(or earlier) at which the values associated with the key is to be returned.
* @return The value to which the specified key is mapped at the specified time or earlier, or
* {@code null} if this map contains no mapping for the key at the specified time or earlier.
*/
public V get(K key, T time) {
try {
return map.get(key).floorEntry(time).getValue();
} catch (NullPointerException ex) {
return null;
}
}
}
欢迎改进和建议 以下是我的实现:
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
/**
* An Implementation a Map with the notion of Time.
*
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
* @param <T> the type of time tracked by this map.
* T MUST implement {@link Comparable} to have a notion of comparing times.
* @author Hari Krishnan
* @see Map
* @see NavigableMap
*/
public class TimedMap<K, V, T extends Comparable<T>> {
private final Map<K, NavigableMap<T, V>> map = new HashMap<>();
/**
* Associates the specified value with the specified key at the specified time in this map.
* If the map previously contained a mapping for the key at this time,
* the old value is replaced by the specified value.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @param time time at which said association should occur
*/
public void set(K key, V value, T time) {
if (!map.containsKey(key))
map.put(key, new TreeMap<>());
map.get(key).put(time, value);
}
/**
* Returns the value to which the specified key is mapped at the specified time or earlier,
* or {@code null} if this map contains no mapping for the key at the specified time or earlier.
*
* @param key the key whose associated value is to be returned
* @param time the time(or earlier) at which the values associated with the key is to be returned.
* @return The value to which the specified key is mapped at the specified time or earlier, or
* {@code null} if this map contains no mapping for the key at the specified time or earlier.
*/
public V get(K key, T time) {
try {
return map.get(key).floorEntry(time).getValue();
} catch (NullPointerException ex) {
return null;
}
}
}
欢迎改进和建议 而不是整个如果!map.containskey。。。您可以用一行代码替换set方法中的代码:map.computeIfAbsentkey,k->new TreeMap.puttime,value。如果您使用的是Java8+Oh-wow。我正在学习数据结构,但不知道它的存在!谢谢不要以这种方式捕获NullPointerException。这将隐藏bug。使用显式null检查并不复杂。虽然我同意NullPointerException的全面捕获是不好的,但在我的例子中,返回null的唯一可能位置是getkey或floorEntrytime。在这两种情况下,我都没有通过手动检查a并返回null来获得额外的清晰度。这是我的考虑,因此我觉得像这样的一行程序更具可读性,而且不失清晰。通过查看您的代码,您如何判断HashMap.get调用的hashCode和equals方法K和TreeMap.floorEntry调用的compareTo方法T永远不会抛出NullPointerException?除此之外,您的代码不是一行代码。您的代码是跨越五行的try-catch块。它并不比,例如,var e=map.getOrDefaultkey,Collections.emptyNavigableMap.floorEntrytime简单;返回e==null?null:e.getValue;而不是整个如果!map.containskey。。。您可以用一行代码替换set方法中的代码:map.computeIfAbsentkey,k->new TreeMap.puttime,value。如果您使用的是Java8+Oh-wow。我正在学习数据结构,但不知道它的存在!谢谢不要以这种方式捕获NullPointerException。这将隐藏bug。使用显式null检查并不复杂。虽然我同意NullPointerException的全面捕获是不好的,但在我的例子中,返回null的唯一可能位置是getkey或floorEntrytime。在这两种情况下,我都没有通过手动检查a并返回null来获得额外的清晰度。这是我的考虑,因此我觉得像这样的一行程序更具可读性,而且不失清晰。通过查看您的代码,您如何判断HashMap.get调用的hashCode和equals方法K和TreeMap.floorEntry调用的compareTo方法T永远不会抛出NullPointerException?除此之外,您的代码不是一行代码。您的代码是跨越五行的try-catch块。它并不比,例如,var e=map.getOrDefaultkey,Collections.emptyNavigableMap.floorEntrytime简单;返回e==null?null:e.getValue;所以你想保留这些变化的历史记录?如果是这样的话,如果你明确地说出来,你的问题会更清楚。不,我追踪的不是历史。假设一个传统地图有一个键值。如果现在引入新值,则覆盖现有值。跟踪这些旧值就是跟踪历史。我的要求与此无关。在这里,同一个键可以在不同的时间具有不同的值。在这里,时间可以是任何可比较的类型。所以您想保留更改的历史记录吗?如果是这样的话,如果你明确地说出来,你的问题会更清楚。不,我追踪的不是历史。假设一个传统地图有一个键值。如果现在引入新值,则覆盖现有值。跟踪这些旧值就是跟踪历史。我的要求与此无关。在这里,同一个键可以在不同的时间具有不同的值。在这里,时间可以是任何可比较的类型。
d.set(1, 1, 0) // set key 1 to value 1 at time 0
d.set(1, 2, 0) // set key 1 to value 2 at time 0
d.get(1, 0) // get key 1 at time 0 should be 2
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
/**
* An Implementation a Map with the notion of Time.
*
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
* @param <T> the type of time tracked by this map.
* T MUST implement {@link Comparable} to have a notion of comparing times.
* @author Hari Krishnan
* @see Map
* @see NavigableMap
*/
public class TimedMap<K, V, T extends Comparable<T>> {
private final Map<K, NavigableMap<T, V>> map = new HashMap<>();
/**
* Associates the specified value with the specified key at the specified time in this map.
* If the map previously contained a mapping for the key at this time,
* the old value is replaced by the specified value.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @param time time at which said association should occur
*/
public void set(K key, V value, T time) {
if (!map.containsKey(key))
map.put(key, new TreeMap<>());
map.get(key).put(time, value);
}
/**
* Returns the value to which the specified key is mapped at the specified time or earlier,
* or {@code null} if this map contains no mapping for the key at the specified time or earlier.
*
* @param key the key whose associated value is to be returned
* @param time the time(or earlier) at which the values associated with the key is to be returned.
* @return The value to which the specified key is mapped at the specified time or earlier, or
* {@code null} if this map contains no mapping for the key at the specified time or earlier.
*/
public V get(K key, T time) {
try {
return map.get(key).floorEntry(time).getValue();
} catch (NullPointerException ex) {
return null;
}
}
}