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

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

HashNode代码如下:

  1. public class HashNode {
  2. private String msg;
  3. private HashNode next;
  4. public String getMsg() {
  5. return msg;
  6. }
  7. public void setMsg(String msg) {
  8. this.msg = msg;
  9. }
  10. public HashNode getNext() {
  11. return next;
  12. }
  13. public void setNext(HashNode next) {
  14. this.next = next;
  15. }
  16. public HashNode(String msg, HashNode next) {
  17. this.msg = msg;
  18. this.next = next;
  19. }
  20. public HashNode() {
  21. }
  22. }

hashset实现如下:

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

测试代码:

  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. System.out.println("包含'你好'? " + myHashSet.contains("你好"));
  14. System.out.println("元素个数: " + myHashSet.size());
  15. myHashSet.display();
  16. myHashSet.remove("hello");
  17. System.out
  18. .println("*****************after remove 'hello'**********************");
  19. myHashSet.display();
  20. System.out.println("元素个数: " + myHashSet.size());
  21. }
  22. }

输出结果:

java中HashSet实现(转)的更多相关文章

  1. Java中HashSet的解读

    一. HashSet源代码 HashSet 的实现   对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的 ...

  2. Java中HashSet和HashMap

    Set中存储元素为什么不重复(即使hashCode相同)? HashSet中存放自定义类型元素时候,需要重写对象中的hashCode方法和equals方法, HashSet中存放自定义类型元素时候,需 ...

  3. Java中HashSet的重复性与判等运算重载

    目录 还有一个故事--(平行世界篇) 还有一个美丽的梦幻家园:java.util 并且还有一个善战的达拉崩巴:HashSet 还有另外一个故事(不是虚假传说) 还有一对涂满毒药的夺命双匕:equals ...

  4. Java中HashSet,HashMap和HashTable的区别

    HashMap.HashSet.HashTable之间的区别是Java程序员的一个常见面试题目,在此仅以此博客记录,并深入源代码进行分析: 在分析之前,先将其区别列于下面 1:HashSet底层采用的 ...

  5. java中HashSet详解(转)

    HashSet 的实现 对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,查看 HashSe ...

  6. java中HashSet详解

    HashSet 的实现 对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,查看 HashSe ...

  7. java集合(4)- java中HashSet详解

    HashSet 的实现 对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,查看 HashSe ...

  8. java中hashSet原理

    转自: http://blog.csdn.net/guoweimelon/article/details/50804799 HashSet是JavaMap类型的集合类中最常使用的,本文基于Java1. ...

  9. java中HashSet对象内的元素的hashCode值不能变化

    因为不管是HashMap(或HashTable,还是HashSet),key值是以hashCode值存进去的,加入key值变了,将无法从集合内删除对象,导致内存溢出.

随机推荐

  1. List<T>类

    List<T>类是ArrayList的泛型等效版本,两者功能相似.它实现了6个接口,实际上市对应的3对. 1.IEnumerable<T>和IEnumerable 2.ICol ...

  2. PYCURL ERROR 6 - “Couldn't resolve host 'mirrorlist.centos.org'”

    在虚拟机上安装的CentOS,估计是网络配置问题,导致yum update和yum install之类的功能的用不了.出现标题上面的错误. ifdown [network_adapter] ifup ...

  3. elasticsearch 搜索不支持单词的部分进行匹配

    zjtest7-frontend:/usr/local/logstash-2.3.4/config# curl -XGET http://192.168.32.80:9200/logstash-201 ...

  4. 百度地图LV1.5实践项目开发工具类bmap.util.jsV1.0

    /** * 百度地图使用工具类-v1.5 * * @author boonya * @date 2013-7-7 * @address Chengdu,Sichuan,China * @email b ...

  5. k-d Tree in TripAdvisor

    Today, TripAdvisor held a tech talk in Columbia University. The topic is about k-d Tree implemented ...

  6. python多线程简单例子

    python多线程简单例子 作者:vpoet mail:vpoet_sir@163.com import thread def childthread(threadid): print "I ...

  7. Linux如何实现开机启动程序详解

    我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤. 加载内核LILO 启动之 ...

  8. 性能优化之Hibernate缓存讲解、应用和调优

    JavaMelody——一款性能监控.调优工具, 通过它让我觉得项目优化是看得见摸得着的,优化有了针对性.而无论是对于分布式,还是非分布,缓存是提示性能的有效工具. 数据层是EJB3.0实现的,而EJ ...

  9. 关于map与set的一点理解;

    set代码: #include<stdio.h> #include<set> using namespace std; int main(){ set<int>m; ...

  10. poj1664 放苹果(递归)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1664 ------ ...