<bdo id='ao0tb'></bdo><ul id='ao0tb'></ul>

  • <small id='ao0tb'></small><noframes id='ao0tb'>

  • <tfoot id='ao0tb'></tfoot>
    <i id='ao0tb'><tr id='ao0tb'><dt id='ao0tb'><q id='ao0tb'><span id='ao0tb'><b id='ao0tb'><form id='ao0tb'><ins id='ao0tb'></ins><ul id='ao0tb'></ul><sub id='ao0tb'></sub></form><legend id='ao0tb'></legend><bdo id='ao0tb'><pre id='ao0tb'><center id='ao0tb'></center></pre></bdo></b><th id='ao0tb'></th></span></q></dt></tr></i><div id='ao0tb'><tfoot id='ao0tb'></tfoot><dl id='ao0tb'><fieldset id='ao0tb'></fieldset></dl></div>

      <legend id='ao0tb'><style id='ao0tb'><dir id='ao0tb'><q id='ao0tb'></q></dir></style></legend>

      1. Java hashCode 不适用于 HashMap?

        Java hashCode doesn#39;t work with HashMap?(Java hashCode 不适用于 HashMap?)
        <legend id='djHTH'><style id='djHTH'><dir id='djHTH'><q id='djHTH'></q></dir></style></legend>

      2. <i id='djHTH'><tr id='djHTH'><dt id='djHTH'><q id='djHTH'><span id='djHTH'><b id='djHTH'><form id='djHTH'><ins id='djHTH'></ins><ul id='djHTH'></ul><sub id='djHTH'></sub></form><legend id='djHTH'></legend><bdo id='djHTH'><pre id='djHTH'><center id='djHTH'></center></pre></bdo></b><th id='djHTH'></th></span></q></dt></tr></i><div id='djHTH'><tfoot id='djHTH'></tfoot><dl id='djHTH'><fieldset id='djHTH'></fieldset></dl></div>
          <bdo id='djHTH'></bdo><ul id='djHTH'></ul>
            <tbody id='djHTH'></tbody>
          1. <tfoot id='djHTH'></tfoot>

                  <small id='djHTH'></small><noframes id='djHTH'>

                  本文介绍了Java hashCode 不适用于 HashMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 HashMap 实现一个稀疏网格,但是似乎覆盖 hashCode() 并不能完全按照我的预期工作.我将我的问题归结为以下代码:

                  I'm attempting to implement a sparse grid using HashMap, however it seems that overriding hashCode() doesn't work quite how I expect. I've boiled down my issue to the following code:

                  public class Main {
                  
                  private static class Coord {
                      int x, y;
                  
                      public Coord(int x, int y) {
                          this.x = x;
                          this.y = y;
                          }
                  
                          @Override
                          public int hashCode() {
                              // See https://en.wikipedia.org/wiki/Pairing_function#Cantor_pairing_function
                              return (((x + y) * (x + y + 1)) / 2) + y;
                          }
                      }
                  
                      public static void main(String[] args) {
                          HashMap<Coord, String> grid = new HashMap<Coord, String>();
                          grid.put(new Coord(0, 0), "A");
                          System.out.println(grid.get(new Coord(0, 0)));
                      }
                  }
                  

                  我希望输出是:

                  A
                  

                  但是,输出是:

                  null
                  

                  两个new Coord(0, 0)"实例都应该返回相同的 hashCode(),但它似乎不像我预期的那样工作.为什么它没有按我的预期工作?

                  Both the "new Coord(0, 0)" instances should return the same hashCode(), but it doesn't seem to work how I expected. Why doesn't it work as I expect?

                  推荐答案

                  一个 HashMap 不能单独在 hashCode 上工作.它也依赖于 equals.

                  A HashMap cannot work on hashCode alone. It relies on equals as well.

                  为了解释原因,让我们考虑一个 HashMap<String, ?>.假设一个人有无限的内存,一个人可以为这个映射创建无限数量的键,但只有 430 万左右可能的哈希码(可能的 int 的数量).因此,会有碰撞.这就是为什么需要 equals 以确保我们获得正确键的值.

                  To explain why, let's consider a HashMap<String, ?>. Supposing for a second that one has infinite memory, one can create an infinite number of keys for this map, but only 4.3 million or so possible hash codes (the number of possible ints). Thus, there will be collisions. This is why equals is required in order to make sure that we're getting the value for the correct key.

                  这篇关于Java hashCode 不适用于 HashMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                  <tfoot id='C2OZB'></tfoot>
                      <tbody id='C2OZB'></tbody>

                      <i id='C2OZB'><tr id='C2OZB'><dt id='C2OZB'><q id='C2OZB'><span id='C2OZB'><b id='C2OZB'><form id='C2OZB'><ins id='C2OZB'></ins><ul id='C2OZB'></ul><sub id='C2OZB'></sub></form><legend id='C2OZB'></legend><bdo id='C2OZB'><pre id='C2OZB'><center id='C2OZB'></center></pre></bdo></b><th id='C2OZB'></th></span></q></dt></tr></i><div id='C2OZB'><tfoot id='C2OZB'></tfoot><dl id='C2OZB'><fieldset id='C2OZB'></fieldset></dl></div>
                    1. <legend id='C2OZB'><style id='C2OZB'><dir id='C2OZB'><q id='C2OZB'></q></dir></style></legend>
                          <bdo id='C2OZB'></bdo><ul id='C2OZB'></ul>

                          • <small id='C2OZB'></small><noframes id='C2OZB'>