手写 HashSet的底层 和 迭代器
1 package Test.CollectionIterator;
2 import java.util.Iterator;
3 public class MyHashSet2<E> implements Iterable<E>{
4 //1.数组+链表 一个add 方法
5 private Node[] arr;
6 private int size;//返回list中的元素个数
7 public int size() {
8 return size;
9 }
10 public boolean add(E e) {
11 boolean flag = false;
12 //1.判断arr数组是否存在 不存在就创建 存在就添加
13 if (arr == null) {
14 arr = new Node[16];//1.1不存在 创建
15 }
16 //1.2 添加
17 //2.根据分组的下标判断 数组下标处 有没有node元素
18 int index = e.hashCode() % 16;
19 if (arr[index] == null) {
20 arr[index] = new Node(e, null);//2.1没有直接创建
21 size++;
22 flag = true;
23 } else {//2.2有的话比较
24 //3.如果hash值和equals值相等 就不添加 否则添加
25 Node node = arr[index];
26 E e1 = (E)node.getE();
27 Node next;
28 do {//3.1循环判读是否相等相等就不添加
29 if (e1.hashCode() == e.hashCode() && e1.equals(e)) {
30 return flag;//相等不添加
31 }//
32 next = node.getNext();
33 if (next != null) {
34 e1 = (E)next.getE();
35 }
36 } while (next != null);//这步是比较完数组某个链表上的全部元素
37 //3.2不相等 开始添加
38 arr[index] = new Node(e, node);//2.1没有直接创建
39 size++;
40 flag = true;
41 }
42 return flag;
43 }
44 @Override
45 public Iterator<E> iterator() {
46 return new Itr();
47 }
48 private class Itr<E> implements Iterator{
49 private int curser;//记录 节点数组 的下标
50 private int count;
51 private Node currentNode;
52
53 @Override
54 public boolean hasNext() {
55 return count!=size;
56 }
57
58 @Override
59 public E next() {
60 if (count>=size){
61 throw new RuntimeException("没有找到下一个");
62 }
63 currentNode=arr[curser];
64 if(currentNode==null){
65 while (true){
66 curser++;
67 if(curser>16)throw new RuntimeException("没有找到下一个");
68 currentNode=arr[curser];
69 if(currentNode==null){
70 curser++;
71 }else {
72 count++;
73 return (E) currentNode.getE();
74 }
75 }
76 }else{
77 count++;
78 Node now =currentNode;
79 if (currentNode.getNext()==null){
80 curser++;
81 }else {
82 currentNode=currentNode.getNext();
83 }
84 return (E)now.getE();
85 /* count++;
86 if (currentNode.getNext()==null){
87 curser++;
88 }else {
89 currentNode=currentNode.getNext();
90 }//这刚好链表上只有一个元素 没有修改currentNode 自己写bug
91 return currentNode.getE();*/
92 }
93 }
94 }
95 // 该内部类 实现 链表结构
96 private class Node<E> {
97 private E e;
98 private Node next;
99 public Node() {
100 }
101 public Node(E e, Node next) {
102 this.e = e;
103 this.next = next;
104 }
105 public E getE() {
106 return e;
107 }
108 public void setE(E e) {
109 this.e = e;
110 }
111 public Node<E> getNext() {
112 return next;
113 }
114 public void setNext(Node<E> next) {
115 this.next = next;
116 }
117 }
118 }
手写 HashSet的底层 和 迭代器的更多相关文章
- 利用递归,反射,注解等,手写Spring Ioc和Di 底层(分分钟喷倒面试官)了解一下
再我们现在项目中Spring框架是目前各大公司必不可少的技术,而大家都知道去怎么使用Spring ,但是有很多人都不知道SpringIoc底层是如何工作的,而一个开发人员知道他的源码,底层工作原理,对 ...
- 算法是什么(二)手写个链表(java)
算法是什么(二)手写个链表(java) liuyuhang原创,未经允许禁止转载 目录 算法是什么(〇) 很多语言的API中都提供了链表实现,或者扩展库中实现了链表. 但是更多的情况下,Map(或 ...
- 6 手写Java LinkedHashMap 核心源码
概述 LinkedHashMap是Java中常用的数据结构之一,安卓中的LruCache缓存,底层使用的就是LinkedHashMap,LRU(Least Recently Used)算法,即最近最少 ...
- 1 手写Java ArrayList核心源码
手写ArrayList核心源码 ArrayList是Java中常用的数据结构,不光有ArrayList,还有LinkedList,HashMap,LinkedHashMap,HashSet,Queue ...
- Mybatis(一):手写一套持久层框架
作者 : 潘潘 未来半年,有幸与导师们一起学习交流,趁这个机会,把所学所感记录下来. 「封面图」 自毕业以后,自己先创业后上班,浮沉了近8年,内心着实焦躁,虽一直是走科班路线,但在技术道路上却始终没静 ...
- Android 手写Binder 教你理解android中的进程间通信
关于Binder,我就不解释的太多了,网上一搜资料一堆,但是估计还是很多人理解的有困难.今天就教你如何从 app层面来理解好Binder. 其实就从我们普通app开发者的角度来看,仅仅对于androi ...
- 【Xamarin挖墙脚系列:代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧(转)】
正愁如何选择构建项目中的视图呢,现在官方推荐画板 Storybord...但是好像 xib貌似更胜一筹.以前的老棒子总喜欢装吊,用代码写....用代码堆一个HTML页面不知道你们尝试过没有.等页面做出 ...
- 利用神经网络算法的C#手写数字识别
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 下载Demo - 2.77 MB (原始地址):handwritten_character_recognition.zip 下载源码 - 70. ...
- AI应用开发实战 - 手写算式计算器
扩展手写数字识别应用 识别并计算简单手写数学表达式 主要知识点 了解MNIST数据集 了解如何扩展数据集 实现手写算式计算器 简介 本文将介绍一例支持识别手写数学表达式并对其进行计算的人工智能应用的开 ...
- 【spring】-- 手写一个最简单的IOC框架
1.什么是springIOC IOC就是把每一个bean(实体类)与bean(实体了)之间的关系交给第三方容器进行管理. 如果我们手写一个最最简单的IOC,最终效果是怎样呢? xml配置: <b ...
随机推荐
- 第二次python作业
#3.1 print("今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问几何?\n") number = int(input("请输入你认为符合条件的数: & ...
- Python Boolean类型 判断
and 判断非Boolean类型数据会自动转换类型 "A" and "B" → "B" 因为表达式 A 和 B都为True所以返回 &quo ...
- (二).JavaScript的运算符和表达式,数据类型转化
4. 运算符和表达式 4.3 赋值运算符和表达式 1.赋值运算符 = 作用:赋值运算符就是将右边的内容赋值给左边的变量或属性. var result = 1 + 2; 2.复合赋值运算符 +=,-=, ...
- 简单的helloworld指令
Hello World指令 1.下载安装notepad++ 2.新建文本文档 3.鼠标右键单击Edit with Noteapd++ 编辑程序并保存 public class hello{ publi ...
- beamforming源码标记
p:各阵元的声压信号矩阵 R:接收数据的自协方差矩阵 Pcbf:交叉谱矩阵
- 子接口vlan终结
问题: 在华为的NE40E设备上,配置三层字接口的ip后,接口的physical状态为up, protocol为down,接口还是不可用. 解决方案: 需要在子接口上配置vlan终结,配置如下: 为什 ...
- 树莓派利用摄像头实现web在线监控
1.https://shumeipai.nxez.com/2021/10/21/raspberry-pi-usb-camera-to-realize-remote-network-monitoring ...
- 获取UndeclaredThrowableException异常信息
一.堆栈错误信息如下,要获取红框里的message 说明:ValidationException为自定义异常,继承自Exception 二.代码如下
- Mysql 行号+分组行号+取Top记录 SQL
Mysql 行号+分组行号+取Top记录 SQL select * from ( SELECT (@rowNum := @rowNum + 1) as rowNum -- 全量行号 , a.col1 ...
- P3128 [USACO15DEC]Max Flow P(树上倍增和树链剖分)
思路1(树上倍增$ + $树上差分) 每次都修改一条从\(u\)到\(v\),不就是树上差分的专门操作吗?? 直接用倍增求\(LCA\),每次\(d[u]++,d[v]++,d[LCA(u,v)]-- ...