ITeye博客

链地址法处理Hash冲突

ArronZhen · 2012-10-29 08:47 · Classical Finding · 编程语言 · Markdown

链地址法处理Hash冲突

java 数据结构 算法  

   哈希表中的每个位置称为桶(bucket),当发生哈希冲突时就以链表形式存放多个元素。

链地址法处理Hash冲突,看看下面代码,模拟了JDK中的HashSet:

Java代码  收藏代码

  1. class  Node{ //节点数据结构
  2. private  Object value; //节点的值
  3. private  Node next; //链表中指向下一结点的引用
  4. /*提供了常见的操作*/
  5. public  Node(Object value){ this .value = value;};
  6. public  Object getValue() { return  value;}
  7. public  Node getNext() { return  next;}
  8. public   void  setNext(Node next){ this .next=next;}
  9. }
  10. public   class  MyHashSet { //Hash数据结构
  11. private  Node[] array; //存储数据链表的数组
  12. private   int  size =  0 ; //表示集合中存放的对象的数目
  13. public  MyHashSet( int  length){
  14. array = new  Node[length]; //创建数组
  15. }
  16. public   int  size(){
  17. return  size;
  18. }
  19. private   static   int  hash (Object o){     //根据对象的哈希码得到一个优化的哈希码,
  20. //算法参照java.util.HashMap的hash()方法
  21. int  h = o.hashCode();
  22. h += ~(h<<9 );
  23. h ^= (h>>>14 );
  24. h += (h<<4 );
  25. h ^= (h>>>10 );
  26. return  h;
  27. }
  28. private   int  indexFor( int  hashCode){     //根据Hash码得到其索引位置
  29. //算法参照java.util.HashMap的indexFor()方法
  30. return  hashCode & (array.length- 1 );
  31. }
  32. public   void  add(Object value) { //把对象加入集合,不允许加入重复的元素
  33. int  index = indexFor(hash(value)); //先根据value得到index
  34. System.out.println("index:"  + index +  " value:"  + value);
  35. Node newNode = new  Node(value); //由value创建一个新节点newNode
  36. Node node = array[index];//由index得到一个节点node
  37. if  (node ==  null ) { //若这个由index得到的节点是空,则将新节点放入其中
  38. array[index]=newNode;
  39. size++;
  40. } else  {
  41. //若不为空则遍历这个点上的链表(下一个节点要等于空或者该节点不等于新节点的值--不允许重复)
  42. Node nextNode;
  43. while  (!node.getValue().equals(value) && (nextNode = node.getNext())!= null ) {
  44. node = nextNode;
  45. }
  46. if  (!node.getValue().equals(value)) {
  47. //若值不相等则加入新节点
  48. node.setNext(newNode);
  49. size++;
  50. }
  51. }
  52. }
  53. public   boolean  contains(Object value){
  54. int  index = indexFor(hash(value));
  55. Node node = array[index];
  56. while  (node!= null  && !node.getValue().equals(value)) {
  57. node = node.getNext();
  58. }//横向查找
  59. if  (node!= null  && node.getValue().equals(value)) {
  60. return   true ;
  61. } else  {
  62. return   false ;
  63. }
  64. }
  65. public   boolean  remove(Object value) {
  66. int  index = indexFor(hash(value));
  67. Node node = array[index];
  68. if  (node!= null  && node.getValue().equals(value)) { //若是第一个节点就是要remove的
  69. array[index]=node.getNext();
  70. size--;
  71. return   true ;
  72. }
  73. Node lastNode = null ;
  74. while  (node!= null  && !node.getValue().equals(value)) { //若不是第一个节点就横向查找
  75. lastNode = node;//记录上一个节点
  76. node = node.getNext();
  77. }
  78. if  (node!= null  && node.getValue().equals(value)) {
  79. lastNode.setNext(node.getNext());
  80. size--;
  81. return   true ;
  82. }else  {
  83. return   false ;
  84. }
  85. }
  86. public  Object[] getAll() {
  87. Object [] values = new  Object[size];
  88. int  index =  0 ;
  89. for  ( int  i =  0 ; i < array.length; i++) {
  90. Node node = array[i];
  91. while  (node!= null ) {
  92. values[index++]=node.getValue();
  93. node = node.getNext();
  94. }
  95. }
  96. return  values;
  97. }
  98. public   static   void  main(String[] args) {
  99. MyHashSet set = new  MyHashSet( 6 );
  100. Object [] values = {"Tom" , "Mike" , "Mike" , "Jack" , "Mary" , "Linda" , "Rose" , "Jone" };
  101. for  ( int  i =  0 ; i < values.length; i++) {
  102. set.add(values[i]);
  103. }
  104. set.remove("Mary" );
  105. System.out.println("size=" +set.size());
  106. values = set.getAll();
  107. for  ( int  i =  0 ; i < values.length; i++) {
  108. System.out.println(values[i]);
  109. }
  110. System.out.println(set.contains("Jack" ));
  111. System.out.println(set.contains("Linda" ));
  112. System.out.println(set.contains("Jane" ));
  113. }
  114. }

结果:
index:4 value:Tom
index:1 value:Mike
index:1 value:Mike
index:5 value:Jack
index:5 value:Mary
index:0 value:Linda
index:0 value:Rose
index:0 value:Jone
size=6
Linda
Rose
Jone
Mike
Tom
Jack
true
true
false

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。