Hashtable的get方法返回null
我正在尝试编写推箱子求解器,我的代码可以工作,但计算解决方案需要很多时间。我认为这是因为我使用ArrayList,我尝试使用Hashtable,但是get方法不起作用,
Hashtable < Vertix, ArrayList<Vertix>> Graph;
所以当我填充我的哈希表并使用key来获取列表时,我得到null。
Graph.get(new Vertix(5,3));
然而顶点存在于图中。
我该如何解决这个问题以提高推箱子解算器的速度。
I am trying to code a sokoban solver, My code works, but it takes so much time to calculate the solution. I think it's because I use ArrayList, I tried to use Hashtable but the method get does not work,
Hashtable < Vertix, ArrayList<Vertix>> Graph;
so when I fill my hashtable, and use the key to get the list I get null.
Graph.get(new Vertix(5,3));
however the vertix exists in Graph.
how can I solve this problem to increase the speed of my Sokoban solver.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Vertix
您自己的课程吗?如果是这样,则需要具有与其他匹配值的其他实例进行比较的平等和哈希码方法的定义。否则,您创建的
new
参考在表中不存在。另外,除非您需要线程安全,否则您只需使用hashmap
Is
Vertix
your own class? If so, it needs to have a definition of equals and hashcode methods to be compared against other instances with matching values.Otherwise, the
new
reference you've created doesn't exist in the table.Also, unless you need thread safety, you can just use Hashmap
您应该阅读
hashmap
和hashtable
的javadocs,其中说明了如何执行查找,以及hashcode
and code>和等于< /代码>方法。
您问题的最有可能的解释是您的
vertix
class doe不覆盖object :: equals
和object :: HashCode
。因此,您的类正在继承“平等表示相同的对象” 行为来自java.lang.object
。换句话说,每个vertix
实例不等于其他每个vertix
实例。因此评估
false
。这可以解释为什么graph.get(new Vertix(5,3))
返回false
。解决方案:Override
等于
和hashcode
,以便它们具有适合您应用程序的正确属性。参考:
equals(equals()equals()
合同请注意,您的代码段中有一些样式等错误。
您可能应该使用
hashmap
而不是hashtable
。hashtable
是 notinionally 螺纹安全,但这是以获取和释放每个get
get 和put put put put 。如果您的代码是单线线程,则不必要。相反,如果您的代码是多线程,则
hashtable
可能是并发瓶颈。Graph
是一个变量名称,因此应该从小写字母开始。vertix
可能是拼写错误。图中节点的英语单词是“顶点”,而不是“ vertix”。 (“ Vertix”是GPS手表,多人游戏射击游戏等的商标。)You should read the javadocs for
HashMap
andHashTable
where it explains how lookups are performed, and the requirements on thehashcode
andequals
methods.The most likely explanation for your problem is that your
Vertix
class doe not overrideObject::equals
andObject::hashCode
. Therefore, your class is inheriting "equality means same object" behavior fromjava.lang.Object
. In other words, everyVertix
instance is not equal to every otherVertix
instance. Thusevaluates to
false
. That would explain whyGraph.get(new Vertix(5,3))
returnsfalse
.Solution: override
equals
andhashCode
so that they have the correct properties for your application.Reference:
equals()
andhashCode()
ContractsNote that there are some style, etc errors in your code snippets.
You probably should use
HashMap
instead ofHashtable
.Hashtable
is notionally thread-safe, but this comes at the cost of acquiring and releasing a lock on eachget
andput
. If your code is single threaded, this is unnecessary. Conversely, if your code is multi-threaded, then aHashtable
is liable to be a concurrency bottleneck.Graph
is a variable name, so it should start with a lowercase letter.Vertix
is probably a spelling error. The English word for a node in a graph is "vertex", not "vertix". ("Vertix" is a trademark for a GPS watch, a multiplayer shooter game, etcetera.)