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值变了,将无法从集合内删除对象,导致内存溢出.
随机推荐
- jQuery中append html后绑定事件不起作用
事件一定要紧跟append之后, 否则append元素点击不起作用 $(function(){$('div').append('<ul><li id="appli" ...
- 杭电1010(dfs + 奇偶剪枝)
题目: The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked ...
- Master Theorem
Master theorem provides a solution in asymptotic terms to solve time complexity problem of most divi ...
- python分布式抓取网页
呵呵,前两节好像和python没多大关系..这节完全是贴代码, 这是我第一次写python,很多地方比较乱,主要就看看逻辑流程吧. 对于编码格式确实搞得我头大..取下来页面不知道是什么编码,所以先找c ...
- 怎么查看chrome网络日志
最近在分析一个页面访问慢的问题,在分析的除了wireshark抓包等手段外,还用到了chrome的日志辅助分析 使用 chrome://net-internals/#events 可以打开日志追踪窗口 ...
- Android应用程序组件Content Provider应用实例
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6950440 文简要介绍了Android应用程序 ...
- kaggle之电影评论文本情感分类
电影文本情感分类 Github地址 Kaggle地址 这个任务主要是对电影评论文本进行情感分类,主要分为正面评论和负面评论,所以是一个二分类问题,二分类模型我们可以选取一些常见的模型比如贝叶斯.逻辑回 ...
- 【求出所有最短路+最小割】【多校第一场】【G题】
题意 A从1要追在N的 B 只能走最短的路 问B最少切断多少条路可以让A不能过来 问B最多切断多少条路A还是能过来 对于1 求出1到N的所有最短路的路径,对其求最小割 对于2 求出长度最小的最短路即可 ...
- Javascript进阶篇——( JavaScript内置对象---上-Date,string,charAt,indexOf,split,substring,substr)笔记整理
什么是对象JavaScript 中的所有事物都是对象,如:字符串.数值.数组.函数等,每个对象带有属性和方法.对象的属性:反映该对象某些特定的性质的,如:字符串的长度.图像的长宽等:对象的方法:能够在 ...
- Web App 图片上传编辑器
使用cropper.jqueryUpload插件.Jquery.src-dataurl-canvas-blob文件. @{ ViewBag.Title = "更新头像"; Layo ...