Hashtable的get方法返回null

发布于 2025-01-19 02:48:02 字数 316 浏览 0 评论 0原文

我正在尝试编写推箱子求解器,我的代码可以工作,但计算解决方案需要很多时间。我认为这是因为我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙沙粒小 2025-01-26 02:48:02

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

救星 2025-01-26 02:48:02

您应该阅读hashmaphashtable的javadocs,其中说明了如何执行查找,以及hashcode and code>和等于< /代码>方法。

您问题的最有可能的解释是您的vertix class doe不覆盖object :: equalsobject :: HashCode。因此,您的类正在继承“平等表示相同的对象” 行为来自java.lang.object。换句话说,每个vertix实例不等于其他每个vertix实例。因此

    new Vertix(5, 3).equals(new Vertix(5, 3)) 

评估false。这可以解释为什么graph.get(new Vertix(5,3))返回false


解决方案:Override 等于hashcode,以便它们具有适合您应用程序的正确属性。

参考:


请注意,您的代码段中有一些样式等错误。

  • 您可能应该使用hashmap而不是hashtablehashtable notinionally 螺纹安全,但这是以获取和释放每个get get 和put put put put 。如果您的代码是单线线程,则不必要。相反,如果您的代码是多线程,则hashtable可能是并发瓶颈。


  • Graph是一个变量名称,因此应该从小写字母开始。

  • vertix可能是拼写错误。图中节点的英语单词是“顶点”,而不是“ vertix”。 (“ Vertix”是GPS手表,多人游戏射击游戏等的商标。)

You should read the javadocs for HashMap and HashTable where it explains how lookups are performed, and the requirements on the hashcode and equals methods.

The most likely explanation for your problem is that your Vertix class doe not override Object::equals and Object::hashCode. Therefore, your class is inheriting "equality means same object" behavior from java.lang.Object. In other words, every Vertix instance is not equal to every other Vertix instance. Thus

    new Vertix(5, 3).equals(new Vertix(5, 3)) 

evaluates to false. That would explain why Graph.get(new Vertix(5,3)) returns false.


Solution: override equals and hashCode so that they have the correct properties for your application.

Reference:


Note that there are some style, etc errors in your code snippets.

  • You probably should use HashMap instead of Hashtable. Hashtable is notionally thread-safe, but this comes at the cost of acquiring and releasing a lock on each get and put. If your code is single threaded, this is unnecessary. Conversely, if your code is multi-threaded, then a Hashtable 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文