如何引用实时地图值

时间:2016-03-31 17:23:31

标签: java dictionary reference

我有一个Map,其中包含我想要快速轻松访问的值。随着get(key)越来越大,使用Map可能会很昂贵。每次我需要时,我都不愿意get(key)。但我也希望通过更改地图来更新它。

private int i = get("i"); // This will not be aware of later changes to the Map

private int i() {
    return get(""); // This is potentially slow
}

private Entry<String,Integer> i; // This skips the key-matching part, and is aware of value changes, but unaware of removal

我认为以上3个选项都不是理想选择。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以使用实现Map的{​​{1}},例如this answer。如果您的地图无法存储PropertyChangeListener值,则可以通过将属性更改为null来表示删除。或者您可以实现自己的null界面。

在任何情况下,都会向已注册的任何一方发出变更信号。它没有返回对地图值的“实时”引用,但至少您不必继续轮询地图以查找更改的值。