java 基于对象引用的Java映射?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5797373/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:47:56  来源:igfitidea点击:

Java map based on object reference?

java

提问by rybosome

So, I'd like to use a java map where the keys are an object...but rather than keying on the object's value, they key on the object ID. So, something like the following would be totally valid code:

所以,我想使用一个 java 映射,其中键是一个对象......但不是键入对象的值,而是键入对象 ID。因此,类似以下内容将是完全有效的代码:

Map<String, Integer> map = new HashMap<String, Integer>();

String s1 = "hi!";
String s2 = "hi!";

map.put(s1, 10);
map.put(s2, 47);

Is this possible? Is there a simple way to do this without building an object ID or something overly cumbersome in my class? Basically, I need a way to associate an ever-changing list of values with a given object. This list of values will potentially be different for objects that have the same value, hence why the default map doesn't work. Other than refactoring my class to do this myself (not really an option, given the time) is there anything that I could use?

这可能吗?有没有一种简单的方法可以做到这一点,而无需在我的班级中构建对象 ID 或过于繁琐的事情?基本上,我需要一种将不断变化的值列表与给定对象相关联的方法。对于具有相同值的对象,此值列表可能会有所不同,因此默认映射不起作用。除了重构我的类来自己做这件事(考虑到时间,这不是一个真正的选择)还有什么我可以使用的吗?

Thanks.

谢谢。

EDIT: Further information.

编辑:更多信息。

The example above was just an example. What I will be using this for is the implementation of a Uniform-Cost search algorithm. For any given node in a search with this algorithm, one must also have the path that has been taken so far. The reason a value-based hash map doesn't work is that this algorithm can reiterate over already-explored nodes. The paths would be different at this point, although the value of "where am I now?" is identical.

上面的例子只是一个例子。我将使用它来实现 Uniform-Cost 搜索算法。对于使用此算法进行搜索的任何给定节点,还必须具有到目前为止所采用的路径。基于值的哈希映射不起作用的原因是该算法可以在已经探索过的节点上重复。在这一点上路径会有所不同,尽管“我现在在哪里?”的价值。是相同的。

回答by Sebastian Zarnekow

I think IdentityHashMap will do the trick. However, both strings will point to the very same instance since you used a string literal. Try s1 = new String("hi!") and s2 = new String("hi!") together with an IdentityHashMap instead.

我认为 IdentityHashMap 可以解决问题。但是,由于您使用了字符串文字,因此两个字符串都将指向同一个实例。尝试将 s1 = new String("hi!") 和 s2 = new String("hi!") 与 IdentityHashMap 一起使用。

回答by Matt Ball

Check out Guava's Multimaps(implementations listed on the Multimapinterface page).

查看GuavaMultimapsMultimap接口页面上列出的实现)。

回答by helpermethod

You should have a look at IdentityHashMap.

你应该看看IdentityHashMap

This class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values).

此类使用哈希表实现 Map 接口,在比较键(和值)时使用引用相等代替对象相等。

回答by mschayna

Just for completness: Guava'sMapMakermakes maps with identity equivalence as a default for weak and soft keys. Look at code here

只是为了完整性:Guava'sMapMaker使映射具有身份等效性作为弱键和软键的默认值。看这里的代码

回答by gd1

You are searching for a multi-map. Java Collection Framework doesn't give you this. But you can mimic it associating a LinkedList(eventually with a single element) to every key. It's very easy.

您正在搜索多地图。Java Collection Framework 没有给你这个。但是您可以模仿它将 a LinkedList(最终与单个元素)关联到每个键。这很容易。