当前位置:网站首页>【集合】HashSet和ArrayList的查找Contains()时间复杂度
【集合】HashSet和ArrayList的查找Contains()时间复杂度
2022-08-10 11:42:00 【檀越剑指大厂】
HashSet和ArrayList的查找Contains()时间复杂度
ArrayList本质就是通过数组实现的,查找一个元素是否包含要用到遍历,时间复杂度是O(N)
HashSetHashSet的查找是通过HashMap的KeySet来实现的,判断是否包含某个元素的实现,时间复杂度是O(1)
//ArrayList判断是否包含某个元素的源码实现:
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++) //从头遍历
if (o.equals(elementData[i]))
return i;
}
return -1;
}
//HashSet判断是否包含某个元素的源码实现:
public boolean contains(Object o) {
return map.containsKey(o);
}
public boolean containsKey(Object key) {
return getNode(hash(key), key) != null;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
//直接通过hash确定元素位置,不用从头遍历
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))//部分情况下可能会继续遍历链表定位
return e;
} while ((e = e.next) != null);
}
}
return null;
}
边栏推荐
- IM即时通讯开发WebSocket从入门到精通
- 个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常
- LeetCode 138. Copy a linked list with random pointers
- 皕杰报表在传参乱码
- Stroke Practice - 62 Valid Sudokus
- Analysis of the name matching process between the LCD driver and the device (Tiny4412)
- 传三星3nm斩获第二家客户,目前产能已供不应求
- 吃透Chisel语言.36.Chisel实战之以FIFO为例(一)——FIFO Buffer和Bubble FIFO的Chisel实现
- It is rumored that Samsung 3nm has won the second customer, and the current production capacity is in short supply
- LeetCode 86. 分隔链表
猜你喜欢
three.js模糊玻璃效果
StoneDB Document Bug Hunting Season 1
蚂蚁金服+拼多多+抖音+天猫(技术三面)面经合集助你拿大厂offer
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
LT8911EXB MIPI CSI/DSI转EDP信号转换
Network Fundamentals (Section 1)
dedecms supports one-click import of Word content
你是怎么知道数据库 Htap 能力强弱的?怎么能看出来
面试美团被问到了Redis,搞懂这几个问题,让你轻松吊打面试官
16. Getting Started with Pytorch Lightning
随机推荐
LeetCode 61. Rotating linked list
Dining (网络流)
The author of open source also has a life problem
Stroke Practice - 62 Valid Sudokus
codevs 2370 Small room tree (LCA)
网络套接字(UDP和TCP编程)
HDU 4135: Co-prime (the principle of inclusion and exclusion)
有哪些好用的性能测试工具推荐?性能测试报告收费标准
LeetCode 83. Remove Duplicate Elements in Sorted List
LeetCode 92. Reverse Linked List II
LeetCode 369. Plus One Linked List
Database management tool: dynamic read-write separation
Can CLIP also do segmentation tasks?The University of Göttingen proposed a model CLIPSeg that uses text and image prompts to perform three segmentation tasks at the same time, draining CLIP capabiliti
个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常
Buckle Exercise - 61 Sort by frequency of characters
可视化服务编排在金融APP中的实践
迈矽科推出高性能77GHz毫米波雷达芯片,尚未量产就已获数万颗订单
面试美团被问到了Redis,搞懂这几个问题,让你轻松吊打面试官
搜索--01
16、Pytorch Lightning入门