java 使用默认值初始化 HashMap?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35664800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initializing a HashMap with default values?
提问by Talen Kylon
I'm implementing the A* search algorithm given here, https://en.wikipedia.org/wiki/A*_search_algorithm
我正在实施这里给出的 A* 搜索算法,https://en.wikipedia.org/wiki/A *_search_algorithm
This line indicates we need to initiliaze a map with the default values of INFINITY,
这一行表示我们需要使用 INFINITY 的默认值初始化一个映射,
gScore := map with default value of Infinity
So I tried that here,
所以我在这里尝试,
Map<State, Double> gScore = new HashMap<State, Double>(Double.POSITIVE_INFINITY);
This does not work however the following does;
这行不通,但以下方法行不通;
Map<State, Double> gScore = new HashMap<State, Double>((int) Double.POSITIVE_INFINITY);
I'm wondering why, and what impact (if any) it will have on my implementation.
我想知道为什么,以及它会对我的实现产生什么影响(如果有的话)。
采纳答案by Louis Wasserman
There is no way to initialize a map with a default value in Java, and your second version will not create a map with a default value of infinity, but instead will try to create an infinitely large map. (Not really, but it'll try creating the largest map possible.)
在 Java 中无法使用默认值初始化地图,您的第二个版本不会创建默认值为无穷大的地图,而是会尝试创建无限大的地图。(不是真的,但它会尝试创建尽可能大的地图。)
Instead, modify the algorithm: anytime you do map.get(key)
, check if the value is null and, if so, replace it with infinity.
相反,修改算法:无论何时map.get(key)
,检查该值是否为空,如果是,则将其替换为无穷大。
回答by Stephen C
I concur with @Louis that the bestsolution is to check the result of the get
call to see if it is null
.
我同意@Louis 的观点,即最好的解决方案是检查get
调用的结果是否为null
.
However, it is also possibleto create a subclass of HashMap
with an override for the get
method that returns a default value if super.get(key)
returns null
. But beware of anomalies; e.g.
然而,也有可能产生的子类HashMap
具有的覆写get
返回如果默认值方法super.get(key)
的回报null
。但要注意异常情况;例如
Iterating the map will give you only the entries that are "really there".
If there is a real entry with a
null
value (because you calledput(key, null)
) you won't get anull
when you callget
for the entries key. But but the entry willshow up in the iteration ... with valuenull
.
迭代地图只会给你“真的存在”的条目。
如果有一个带有
null
值的真实条目(因为您调用了put(key, null)
),则null
在您调用get
条目键时将不会得到 a 。但是,该条目将出现在迭代中 ... 带有 valuenull
。
So, from an OO design perspective, a better approach (to extending HashMap
) would be to create an application-specific class that only exposes a subset of the Map
functionality.
因此,从 OO 设计的角度来看,更好的方法(扩展HashMap
)是创建一个特定于应用程序的类,该类只公开Map
功能的一个子集。
回答by Eran
Instead of explicitly putting Double.POSITIVE_INFINITY
for all the possible keys (which may be inefficient), you can use putIfAbsent
.
相反的明确把Double.POSITIVE_INFINITY
所有可能的密钥(这可能是低效的),你可以使用putIfAbsent
。
In any place where you have:
在您拥有的任何地方:
value = map.get(key);
you can change it to:
您可以将其更改为:
value = map.putIfAbsent(key,Double.POSITIVE_INFINITY);
This will behave as if the value of any key not present in the Map
(or any key whose current value is null
) is Double.POSITIVE_INFINITY
. It will put the value Double.POSITIVE_INFINITY
in the Map
for any such keys.
这将表现得好像 中不存在的Map
任何键的值(或当前值为 的任何键null
)是Double.POSITIVE_INFINITY
。它会将值Double.POSITIVE_INFINITY
放入Map
任何此类键的 中。
It will save you the need to check if map.get(key)
returns null
.
它将使您无需检查是否map.get(key)
返回null
。