免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1603 | 回复: 1
打印 上一主题 下一主题

Java HashMap冲突实例 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-31 16:25 |只看该作者 |倒序浏览
Java HashMap冲突实例






参考:PHP数组的Hash冲突实例 http://www.laruence.com/2011/12/30/2435.html


看到这篇帖子,其实数据结构真实的存在于身边。模仿上文,弄个Java版的。

1、重写hashcode,最好(一定)要重写equals。即hashcode相同则equals返回true


Java代码
  1. 1.import java.util.HashMap;   
  2. 2.  
  3. 3.public class TestWorstHashMap {   
  4. 4.  
  5. 5.    private static final int testSize = 100000;   
  6. 6.    private static final int specialId = 1;   
  7. 7.    private static final String specialName = "1";   
  8. 8.  
  9. 9.    /************************************ test1 **************************************/  
  10. 10.  
  11. 11.    class BadModel {   
  12. 12.        private int id;   
  13. 13.        private String name;   
  14. 14.  
  15. 15.        public BadModel(int id, String name) {   
  16. 16.            this.id = id;   
  17. 17.            this.name = name;   
  18. 18.        }   
  19. 19.  
  20. 20.        /**  
  21. 21.         * @see java.util.HashMap.put(K, V)  
  22. 22.         */  
  23. 23.        @Override  
  24. 24.        public boolean equals(Object obj) {   
  25. 25.            if (obj instanceof BadModel) {   
  26. 26.                return id == ((BadModel) obj).id && name.equals(((BadModel) obj).name);   
  27. 27.            }   
  28. 28.  
  29. 29.            return false;   
  30. 30.        }   
  31. 31.  
  32. 32.        @Override  
  33. 33.        public int hashCode() {   
  34. 34.            return id;   
  35. 35.        }   
  36. 36.    }   
  37. 37.  
  38. 38.    void testBadModel1() {   
  39. 39.        HashMap<BadModel, Object> map = new HashMap<BadModel, Object>();   
  40. 40.  
  41. 41.        long start = System.currentTimeMillis();   
  42. 42.        for (int i = 0; i < testSize; i++) {   
  43. 43.            BadModel model = new BadModel(specialId, "Name" + i);   
  44. 44.            map.put(model, "test" + i);   
  45. 45.        }   
  46. 46.  
  47. 47.        long end = System.currentTimeMillis();   
  48. 48.        System.out.println("BadModel the same id  : " + (end - start));   
  49. 49.    }   
  50. 50.  
  51. 51.    void testBadModel2() {   
  52. 52.        HashMap<BadModel, Object> map = new HashMap<BadModel, Object>();   
  53. 53.  
  54. 54.        long start = System.currentTimeMillis();   
  55. 55.        for (int i = 0; i < testSize; i++) {   
  56. 56.            BadModel model = new BadModel(specialId, specialName);   
  57. 57.            map.put(model, "test" + i);   
  58. 58.        }   
  59. 59.        long end = System.currentTimeMillis();   
  60. 60.  
  61. 61.        System.out.println("BadModel the same id & name : " + (end - start));   
  62. 62.    }   
  63. 63.  
  64. 64.    /************************************ test2 **************************************/  
  65. 65.  
  66. 66.    class GoodModel {   
  67. 67.        private int id;   
  68. 68.        private String name;   
  69. 69.  
  70. 70.        public GoodModel(int id, String name) {   
  71. 71.            this.id = id;   
  72. 72.            this.name = name;   
  73. 73.        }   
  74. 74.  
  75. 75.        @Override  
  76. 76.        public boolean equals(Object obj) {   
  77. 77.            if (obj instanceof GoodModel) {   
  78. 78.                return id == ((GoodModel) obj).id && name.equals(((GoodModel) obj).name);   
  79. 79.            }   
  80. 80.  
  81. 81.            return false;   
  82. 82.        }   
  83. 83.  
  84. 84.        @Override  
  85. 85.        public int hashCode() {   
  86. 86.            return id + name.hashCode();   
  87. 87.        }   
  88. 88.    }   
  89. 89.  
  90. 90.    void testGoodModel() {   
  91. 91.        HashMap<GoodModel, Object> map = new HashMap<GoodModel, Object>();   
  92. 92.  
  93. 93.        long start = System.currentTimeMillis();   
  94. 94.        for (int i = 0; i < testSize; i++) {   
  95. 95.            GoodModel model = new GoodModel(specialId, "Name" + i);   
  96. 96.            map.put(model, "test" + i);   
  97. 97.        }   
  98. 98.  
  99. 99.        long end = System.currentTimeMillis();   
  100. 100.        System.out.println("GoodModel the same id  : " + (end - start));   
  101. 101.    }   
  102. 102.  
  103. 103.    public static void main(String[] args) {   
  104. 104.        TestWorstHashMap test = new TestWorstHashMap();   
  105. 105.        test.testBadModel1();   
  106. 106.        test.testBadModel2();   
  107. 107.        test.testGoodModel();   
  108. 108.    }   
  109. 109.  
  110. 110.}  
  111. import java.util.HashMap;

  112. public class TestWorstHashMap {

  113.         private static final int testSize = 100000;
  114.         private static final int specialId = 1;
  115.         private static final String specialName = "1";

  116.         /************************************ test1 **************************************/

  117.         class BadModel {
  118.                 private int id;
  119.                 private String name;

  120.                 public BadModel(int id, String name) {
  121.                         this.id = id;
  122.                         this.name = name;
  123.                 }

  124.                 /**
  125.                  * @see java.util.HashMap.put(K, V)
  126.                  */
  127.                 @Override
  128.                 public boolean equals(Object obj) {
  129.                         if (obj instanceof BadModel) {
  130.                                 return id == ((BadModel) obj).id && name.equals(((BadModel) obj).name);
  131.                         }

  132.                         return false;
  133.                 }

  134.                 @Override
  135.                 public int hashCode() {
  136.                         return id;
  137.                 }
  138.         }

  139.         void testBadModel1() {
  140.                 HashMap<BadModel, Object> map = new HashMap<BadModel, Object>();

  141.                 long start = System.currentTimeMillis();
  142.                 for (int i = 0; i < testSize; i++) {
  143.                         BadModel model = new BadModel(specialId, "Name" + i);
  144.                         map.put(model, "test" + i);
  145.                 }

  146.                 long end = System.currentTimeMillis();
  147.                 System.out.println("BadModel the same id  : " + (end - start));
  148.         }

  149.         void testBadModel2() {
  150.                 HashMap<BadModel, Object> map = new HashMap<BadModel, Object>();

  151.                 long start = System.currentTimeMillis();
  152.                 for (int i = 0; i < testSize; i++) {
  153.                         BadModel model = new BadModel(specialId, specialName);
  154.                         map.put(model, "test" + i);
  155.                 }
  156.                 long end = System.currentTimeMillis();

  157.                 System.out.println("BadModel the same id & name : " + (end - start));
  158.         }

  159.         /************************************ test2 **************************************/

  160.         class GoodModel {
  161.                 private int id;
  162.                 private String name;

  163.                 public GoodModel(int id, String name) {
  164.                         this.id = id;
  165.                         this.name = name;
  166.                 }

  167.                 @Override
  168.                 public boolean equals(Object obj) {
  169.                         if (obj instanceof GoodModel) {
  170.                                 return id == ((GoodModel) obj).id && name.equals(((GoodModel) obj).name);
  171.                         }

  172.                         return false;
  173.                 }

  174.                 @Override
  175.                 public int hashCode() {
  176.                         return id + name.hashCode();
  177.                 }
  178.         }

  179.         void testGoodModel() {
  180.                 HashMap<GoodModel, Object> map = new HashMap<GoodModel, Object>();

  181.                 long start = System.currentTimeMillis();
  182.                 for (int i = 0; i < testSize; i++) {
  183.                         GoodModel model = new GoodModel(specialId, "Name" + i);
  184.                         map.put(model, "test" + i);
  185.                 }

  186.                 long end = System.currentTimeMillis();
  187.                 System.out.println("GoodModel the same id  : " + (end - start));
  188.         }

  189.         public static void main(String[] args) {
  190.                 TestWorstHashMap test = new TestWorstHashMap();
  191.                 test.testBadModel1();
  192.                 test.testBadModel2();
  193.                 test.testGoodModel();
  194.         }

  195. }
  196.   
复制代码
测试结果:



BadModel the same id  : 141454

BadModel the same id & name : 28

GoodModel the same id  : 92







1、 table为一个Entry[] 数组。
2、以<key, value>的形式存储为一个Entry。
3、相同hashcode的key,以链头插入的方式保存。


从上图看出,我们造的数据都保存在table的一个Entry的链表中了。

论坛徽章:
0
2 [报告]
发表于 2011-12-31 16:42 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP