java中HashSet实现(转)
hashset底层的数据结构是hash表,hash表实现方式采用数组+链表,数组类型为HashNode,每个数组元素为链表的头指针,链表中存储经过hash函数散列后冲突的元素,数组的长度为26
hashset存储的元素类型为字符串,取每个字符串的首字符的ascall码作为hash函数的输入,数组的长度为10,散列函数h(x)=x%10。
HashNode代码如下:
- public class HashNode {
- private String msg;
- private HashNode next;
- public String getMsg() {
- return msg;
- }
- public void setMsg(String msg) {
- this.msg = msg;
- }
- public HashNode getNext() {
- return next;
- }
- public void setNext(HashNode next) {
- this.next = next;
- }
- public HashNode(String msg, HashNode next) {
- this.msg = msg;
- this.next = next;
- }
- public HashNode() {
- }
- }
hashset实现如下:
- public class MyHashSet {
- private HashNode[] nodes = new HashNode[10];
- private int size = 0;
- public MyHashSet() {
- for (int i = 0; i < nodes.length; i++) {
- nodes[i] = new HashNode();
- }
- }
- public boolean add(String value) {
- if (contains(value)) { // 如果有这个元素,就不插入
- return false;
- }
- HashNode node = new HashNode(value, null);
- int index = (int) value.charAt(0) % nodes.length; // 取第一个字符作为hash函数的输入
- // 如果该链为空,直接插入,否则采用头插法
- if (nodes[index].getNext() == null) {
- nodes[index].setNext(node);
- } else {
- node.setNext(nodes[index].getNext());
- nodes[index].setNext(node);
- }
- size++;
- return true;
- }
- public boolean remove(String value) {
- if (!contains(value)) {
- return false;
- }
- int index = (int) value.charAt(0) % nodes.length;
- HashNode node = nodes[index];
- HashNode node2 = node.getNext();
- while (node2 != null) {
- if (node2.getMsg().equals(value)) {
- node.setNext(node2.getNext());
- size--;
- break;
- }
- node = node.getNext();
- node2 = node.getNext();
- }
- return true;
- }
- public void display() {
- for (int i = 0; i < nodes.length; i++) {
- HashNode node = nodes[i].getNext();
- System.out.print(i + " :");
- while (node != null) {
- System.out.print(node.getMsg() + " ");
- node = node.getNext();
- }
- System.out.println();
- }
- }
- public int size() {
- return size;
- }
- public boolean contains(String value) {
- int index = (int) value.charAt(0) % nodes.length;
- HashNode node = nodes[index].getNext();
- while (node != null) {
- if (node.getMsg().equals(value)) {
- return true;
- }
- node = node.getNext();
- }
- return false;
- }
- }
测试代码:
- public class TestMyHashSet {
- public static void main(String[] args) {
- MyHashSet myHashSet = new MyHashSet();
- myHashSet.add("hello");
- myHashSet.add("hey");
- myHashSet.add("apply");
- myHashSet.add("你好");
- myHashSet.add("你是谁");
- myHashSet.add("cat");
- myHashSet.add("dog");
- myHashSet.add("cat");
- myHashSet.add("你好");
- System.out.println("包含'你好'? " + myHashSet.contains("你好"));
- System.out.println("元素个数: " + myHashSet.size());
- myHashSet.display();
- myHashSet.remove("hello");
- System.out
- .println("*****************after remove 'hello'**********************");
- myHashSet.display();
- System.out.println("元素个数: " + myHashSet.size());
- }
- }
输出结果:

java中HashSet实现(转)的更多相关文章
- Java中HashSet的解读
一. HashSet源代码 HashSet 的实现 对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的 ...
- Java中HashSet和HashMap
Set中存储元素为什么不重复(即使hashCode相同)? HashSet中存放自定义类型元素时候,需要重写对象中的hashCode方法和equals方法, HashSet中存放自定义类型元素时候,需 ...
- Java中HashSet的重复性与判等运算重载
目录 还有一个故事--(平行世界篇) 还有一个美丽的梦幻家园:java.util 并且还有一个善战的达拉崩巴:HashSet 还有另外一个故事(不是虚假传说) 还有一对涂满毒药的夺命双匕:equals ...
- Java中HashSet,HashMap和HashTable的区别
HashMap.HashSet.HashTable之间的区别是Java程序员的一个常见面试题目,在此仅以此博客记录,并深入源代码进行分析: 在分析之前,先将其区别列于下面 1:HashSet底层采用的 ...
- java中HashSet详解(转)
HashSet 的实现 对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,查看 HashSe ...
- java中HashSet详解
HashSet 的实现 对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,查看 HashSe ...
- java集合(4)- java中HashSet详解
HashSet 的实现 对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,查看 HashSe ...
- java中hashSet原理
转自: http://blog.csdn.net/guoweimelon/article/details/50804799 HashSet是JavaMap类型的集合类中最常使用的,本文基于Java1. ...
- java中HashSet对象内的元素的hashCode值不能变化
因为不管是HashMap(或HashTable,还是HashSet),key值是以hashCode值存进去的,加入key值变了,将无法从集合内删除对象,导致内存溢出.
随机推荐
- 外贸中MFQ
MFQ = Mask Fee Quantity 退掩膜费量Masking charge USD 2000. MFQ 100k in the first year
- 关于Winsock编程中IO重叠的概念
我在看<Windows网络与通信程序设计>(王艳平)这本书时,对重叠IO很不理解,突然就冒出这么一个概念,没一点头绪.就目前的理解做一个整理. 第一种理解:OVERLAPPED,顾名思义为 ...
- UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>
N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_Can You Help God Wu CDOJ 582
There is a boy named God Wu in UESTC ACM team. One day he is asked to finish a task. The task is tha ...
- netstat和telnet命令在Windows7中的用法(转载)
在网络方面我们常常会用到如下命令: (1)ping命令:我们常常用来判断2台或2台以上的机器间是否网络连通. ping 192.168.1.88 -t 如果想看任何命令的参数是什么意思,我们只需要:命 ...
- Unity 读取Excel
游戏有大多数配置文件,比如玩家等级,游戏商店信息等等.通常情况下把这些放入excel中来读取 第一种解决方案: xlsx –> csv –> 改变成UTF-8 或者Unicode编码 –& ...
- IOS 排序算法
/** * @brief 冒泡排序法 * * @param arr 需要排序的数组 */ -(void)BubbleSort:(NSMutableArray *)arr { // 取第一个与其邻接的对 ...
- Pasha and String(思维,技巧)
Pasha and String Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- 关于Node.js, Jade一点小小的介绍。
本文出自:http://blog.csdn.net/svitter node.js大家知道的可能比較多,可是jade大家可能就不知道了.. GFW封杀掉google以后.今天在百度上找了好久也没有找到 ...
- Oracle SQL函数之转换函数To_char汇总
TO_CHAR(x[[,c2],C3])[功能]将日期或数据转换为char数据类型[参数]x是一个date或number数据类型.c2为格式参数c3为NLS设置参数如果x为日期nlsparm=NLS_ ...