当前位置:网站首页>【集合】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;
}
边栏推荐
- If someone asks you about distributed transactions again, throw this to him
- 常量及数据类型你还记得多少?
- 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
- LeetCode 138. 复制带随机指针的链表
- dedecms支持Word内容一键导入
- Redis常用命令
- HDU 6040 Hints of sd0061 (技巧)
- A detailed explanation of implementation api embed
- Servlet---Solve the problem of Chinese garbled characters in post requests
- Apple bucks the trend and expands iPhone 14 series stocking, with a total of 95 million units
猜你喜欢
制品库是什么?
OPNsense安装配置Zenarmor
mpf6_Time Series Data_quandl_更正kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
Does your child lack self-discipline?Ape Counseling: Pay attention to "blank" in the schedule to give children more control
培训机构学习费用是多少呢?
Database management tool: dynamic read-write separation
人脸考勤是选择人脸比对1:1还是人脸搜索1:N?
You have a Doubaqiong thesaurus, please check it
APP automation testing practice based on UiAutomator2+PageObject mode
随机推荐
LeetCode 369. Plus One Linked List(链表加1)
Dining (网络流)
日记16
搜索--01
彩色图和深度图转点云
Flutter气泡框实现
可视化服务编排在金融APP中的实践
Threshold-based filtering buffer management scheme in a shared buffer packet switch论文核心部分
第5章 虚拟存储器
网络基础(第一节)
再有人问你分布式事务,把这篇扔给他
有哪些好用的性能测试工具推荐?性能测试报告收费标准
Dining (web stream)
一文读懂NFT数字藏品为何风靡全球?
LeetCode 82. Remove Duplicate Elements in Sorted List II
Since the media hot style title how to write?Taught you how to write the title
常量及数据类型你还记得多少?
search--01
Excel function formulas - LOOKUP function
Data Analysis of Time Series (5): Simple Prediction Method