简体   繁体   English

在HashMap中使用相同的对象作为键和值

[英]Use the same object as both key and value in a HashMap

I've re-worded this search about as many times as I think I can and I've come up with nothing, so either this hasn't been asked before, or I don't know HOW to ask. 我已经以我认为可以的方式多次重新搜索了此搜索,但是我什么都没有提出,因此或者以前没有问过这个问题,或者我不知道该怎么问。

I am working on a personal project that boils down to trying to find the shortest path from a starting state to a finishing state. 我正在做一个个人项目,归结为试图找到从开始状态到结束状态的最短路径。

There are too many (more than 2^64) states so I can't generate the entire graph. 状态太多(超过2 ^ 64个),因此无法生成整个图形。 However, each state contains enough information to determine all the states adjacent to it. 但是,每个状态包含足够的信息来确定与其相邻的所有状态。 There are (infinitely) many paths from a state to another, and I am only interested in the shortest. 从一个状态到另一个状态有(无限)多种路径,而我只对最短路径感兴趣。 This requires me to know if I've reached been to a state before, and also how I got there the first time. 这要求我知道我以前是否到达过某个州,以及我如何第一次到达那里。

My state object contains all the state information, as well as the depth of the path that lead me there, and the move I made to get there from the previous state in that path. 我的状态对象包含所有状态信息,以及引导我到达该位置的路径的深度,以及我从该路径中的先前状态到达该位置所做的动作。 If I get to the same state following a different path, the state information will be the same, but the depth and previous move fields will be different. 如果我沿不同的路径进入相同的状态,则状态信息将相同,但是深度和先前的移动字段将不同。

I want a data structure that will tell me if I have visited that state before, and if I have, retrieve the depth and previous state information from it. 我想要一个数据结构,该结构将告诉我是否曾经访问过该状态,如果有,请从中检索深度和以前的状态信息。

The best solution I have come up with so far is to use a HashMap which maps a state to a state, and use the same state object as both the key and value, as in: myHashMap.put(myState, myState) 到目前为止,我想出的最好的解决方案是使用HashMap将状态映射到状态,并使用相同的状态对象作为键和值,例如: myHashMap.put(myState, myState)

I've implemented hashCode() and equals() such that two states will be considered "equal" if their state information is the same (ie we've been in this room before) regardless of how I got there (ie which door I used to enter the room). 我已经实现了hashCode()和equals(),这样无论我如何到达那里(即我走哪扇门),如果两个州的状态信息相同(即我们之前曾在这个房间里),则它们将被视为“相等”。用于进入房间)。

This seems rather silly but I can't think of another way (with fast storage/access) to store the information about whether I've been to a state and how I got there. 这似乎很愚蠢,但我想不出另一种方式(具有快速存储/访问权限)来存储有关我是否去过某个州以及如何到达那里的信息。

Does my plan make sense, or is there a better way? 我的计划有意义吗,还是有更好的方法?

If you see yourself storing key and value as the same value, then it is a Set what you really need. 如果您看到自己将键和值存储为相同的值,那么它就是您真正需要的Set

 Set<State> set = new HashSet<>();
 set.put(stat1);

And by the way your solution is not very far from that as HashSet is backed by a HashMap behind the scenes which means a HashSet has the performance characteristic as those of HashMap and relies on equals and hashCode methods. 顺便说一句,您的解决方案与HashSet背后有一个HashMap支持,因此相距不远,这意味着HashSet具有与HashMap的性能特征,并且依赖equalshashCode方法。

You say that "I want a data structure that will tell me if I have visited that state before, and if I have, retrieve the depth and previous state information from it." 您说“我想要一个数据结构,该结构将告诉我是否曾经访问过该状态,如果有,请从中检索深度和以前的状态信息。”

A Set (HashSet is likely better than a TreeSet for what you've described) will do the first part. 一个Set(就您所描述的而言,HashSet可能比TreeSet更好)将执行第一部分。 Now, I see that you are trying to use a Map in order to do the second half which is retrieving the information. 现在,我看到您正在尝试使用Map来完成检索信息的第二部分。 However, if you are already able to check if you've visited a state, that means you have a reference to the state. 但是,如果您已经能够检查您是否访问过某个州,则意味着您已经对该州有了参考。 So, you don't need a map at all. 因此,您根本不需要地图。

/*  Marking a state as visited  */
Set<State> visited = new HashSet<>();
visited.put(currentState);

/*  Checking if visited/retrieving  */
if (visited.contains(currentState)) {
    // already visited
} else {
    // do something with 'currentState'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM