java中HashSet实现(转)

hashset底层的数据结构是hash表,hash表实现方式采用数组+链表,数组类型为HashNode,每个数组元素为链表的头指针,链表中存储经过hash函数散列后冲突的元素,数组的长度为26

hashset存储的元素类型为字符串,取每个字符串的首字符的ascall码作为hash函数的输入,数组的长度为10,散列函数h(x)=x%10。

HashNode代码如下:

 

[java] view plaincopy
 
  1. public class HashNode {  
  2.     private String msg;  
  3.     private HashNode next;  
  4.   
  5.     public String getMsg() {  
  6.         return msg;  
  7.     }  
  8.   
  9.     public void setMsg(String msg) {  
  10.         this.msg = msg;  
  11.     }  
  12.   
  13.     public HashNode getNext() {  
  14.         return next;  
  15.     }  
  16.   
  17.     public void setNext(HashNode next) {  
  18.         this.next = next;  
  19.     }  
  20.   
  21.     public HashNode(String msg, HashNode next) {  
  22.         this.msg = msg;  
  23.         this.next = next;  
  24.     }  
  25.   
  26.     public HashNode() {  
  27.   
  28.     }  
  29. }  

 

 

hashset实现如下:

 

[java] view plaincopy
 
  1. public class MyHashSet {  
  2.     private HashNode[] nodes = new HashNode[10];  
  3.     private int size = 0;  
  4.   
  5.     public MyHashSet() {  
  6.         for (int i = 0; i < nodes.length; i++) {  
  7.             nodes[i] = new HashNode();  
  8.         }  
  9.     }  
  10.   
  11.     public boolean add(String value) {  
  12.         if (contains(value)) { // 如果有这个元素,就不插入  
  13.             return false;  
  14.         }  
  15.         HashNode node = new HashNode(value, null);  
  16.         int index = (int) value.charAt(0) % nodes.length; // 取第一个字符作为hash函数的输入  
  17.   
  18.         // 如果该链为空,直接插入,否则采用头插法  
  19.         if (nodes[index].getNext() == null) {  
  20.             nodes[index].setNext(node);  
  21.         } else {  
  22.             node.setNext(nodes[index].getNext());  
  23.             nodes[index].setNext(node);  
  24.         }  
  25.         size++;  
  26.         return true;  
  27.     }  
  28.   
  29.     public boolean remove(String value) {  
  30.         if (!contains(value)) {  
  31.             return false;  
  32.         }  
  33.         int index = (int) value.charAt(0) % nodes.length;  
  34.         HashNode node = nodes[index];  
  35.         HashNode node2 = node.getNext();  
  36.         while (node2 != null) {  
  37.             if (node2.getMsg().equals(value)) {  
  38.                 node.setNext(node2.getNext());  
  39.                 size--;  
  40.                 break;  
  41.             }  
  42.             node = node.getNext();  
  43.             node2 = node.getNext();  
  44.         }  
  45.         return true;  
  46.     }  
  47.   
  48.     public void display() {  
  49.         for (int i = 0; i < nodes.length; i++) {  
  50.             HashNode node = nodes[i].getNext();  
  51.             System.out.print(i + " :");  
  52.             while (node != null) {  
  53.                 System.out.print(node.getMsg() + "  ");  
  54.                 node = node.getNext();  
  55.             }  
  56.             System.out.println();  
  57.         }  
  58.     }  
  59.   
  60.     public int size() {  
  61.         return size;  
  62.     }  
  63.   
  64.     public boolean contains(String value) {  
  65.         int index = (int) value.charAt(0) % nodes.length;  
  66.         HashNode node = nodes[index].getNext();  
  67.         while (node != null) {  
  68.             if (node.getMsg().equals(value)) {  
  69.                 return true;  
  70.             }  
  71.             node = node.getNext();  
  72.         }  
  73.         return false;  
  74.     }  
  75. }  


测试代码:

 

 

[java] view plaincopy
 
  1. public class TestMyHashSet {  
  2.     public static void main(String[] args) {  
  3.         MyHashSet myHashSet = new MyHashSet();  
  4.         myHashSet.add("hello");  
  5.         myHashSet.add("hey");  
  6.         myHashSet.add("apply");  
  7.         myHashSet.add("你好");  
  8.         myHashSet.add("你是谁");  
  9.         myHashSet.add("cat");  
  10.         myHashSet.add("dog");  
  11.         myHashSet.add("cat");  
  12.         myHashSet.add("你好");  
  13.   
  14.         System.out.println("包含'你好'? " + myHashSet.contains("你好"));  
  15.         System.out.println("元素个数: " + myHashSet.size());  
  16.         myHashSet.display();  
  17.         myHashSet.remove("hello");  
  18.         System.out  
  19.                 .println("*****************after remove 'hello'**********************");  
  20.         myHashSet.display();  
  21.         System.out.println("元素个数: " + myHashSet.size());  
  22.     }  
  23. }  


输出结果:

 

 

posted @ 2015-11-23 23:11  鞋带Er  阅读(416)  评论(0编辑  收藏  举报