How to Check if an Array Contains a Value in Java Efficiently?---reference
How to check if an array (unsorted) contains a certain value? This is a very useful and frequently used operation in Java. It is also a top voted question on Stack Overflow. As shown in top voted answers, checking if an array contains a certain value can be done in several different ways, but the time complexity could be very different. In the following I will show the time each method takes.
1. 4 Different Ways to Check If an Array Contains a Value
1) Using List:
public static boolean useList(String[] arr, String targetValue) { |
2) Using Set:
public static boolean useSet(String[] arr, String targetValue) { |
3) Using a simple loop:
public static boolean useLoop(String[] arr, String targetValue) { |
4) Using Arrays.binarySearech():
public static boolean useArraysBinarySearch(String[] arr, String targetValue) { |
2. Time Complexity
The approximate time complexity can be compared by using the following code. It is not precise, just search an array of size 5, 1k, 10k, but the idea is clear.
public static void main(String[] args) { |
Result:
useList: 13
useSet: 72
useLoop: 5
useArraysBinarySearch: 9
Use a larger array (1k):
String[] arr = new String[1000]; |
Result:
useList: 112
useSet: 2055
useLoop: 99
useArrayBinary: 12
Use a larger array (10k):
String[] arr = new String[10000]; |
Result:
useList: 1590
useSet: 23819
useLoop: 1526
useArrayBinary: 12
Clearly, using a simple loop method is more efficient than using any collection. A lot of developers use the first method, but it is inefficient. Pushing the array to another Collection type will require spin through all elements to read them in before doing anything with the collection type.
The array must be sorted, if Arrays.binarySearch() method is used. In this case, the array is not sorted, therefore, it should not be used.
Actually, if you really need to check if a value is contained in some array/collection efficiently, a sorted list or tree can do it in O(log(n)) or hashset can do it in O(1).
reference from:http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/
How to Check if an Array Contains a Value in Java Efficiently?---reference的更多相关文章
- [ES2016] Check if an array contains an item using Array.prototype.includes
We often want to check if an array includes a specific item. It's been common to do this with the Ar ...
- leetcode 108 Convert Sorted Array to Binary Search Tree ----- java
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 给一 ...
- Java反射04 : 通过Array动态创建和访问Java数组
java.lang.reflect.Array类提供了通过静态方法来动态创建和访问Java数组的操作. 本文转载自:https://blog.csdn.net/hanchao5272/article/ ...
- 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- Array和String测试与java.String.split
java.string.split() 存在于java.lang包中,返回值是一个数组. 作用是按指定字符或者正则去切割某个字符串,结果以字符串数组形式返回. 例 String [] toSort = ...
- dubbo高级配置学习
启动时检查 可以通过check="false"关闭检查,比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动. 关闭某个服务的启动时检查:(没有提供者时报错) < ...
- dubbo高级配置学习(上)
启动时检查 Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发现问题,默认check=true. 如果你的Spring容器是懒加载的, ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 启动时检查
示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 启动时检查 Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发 ...
随机推荐
- java Ant 的使用
Apache Ant 1.7.0 is the best available version的下载地址:http://ant.apache.org/bindownload.cgi 部署: 参考JAVA ...
- [GRYZ2015]INCR
题目描述 数列 A1,A2,...,AN,修改最少的数字,使得数列严格单调递增. 输入格式 第 1 行,1 个整数 N 第 2 行,N 个整数 A1,A2,...,AN 输出格式 1 个整数,表示最少 ...
- 你认为你很了解Javascript?
(翻译不当之处请谅解) 来源:http://www.ido321.com/914.html 这里有5个小脚本,有助于你真正理解JavaScript核心–闭包和作用域.没有在控制台运行之前,尝试回答每个 ...
- Spark系列(七)Master中的资源调度
资源调度 说明: Application的调度算法有两种,分别为spreadOutApps和非spreadOutApps spreadOutApps 在spark-submit脚本中,可以指定要多少个 ...
- nservicebus教程-目录
表的内容 开始 坚持NServiceBus 扩展 每天 举办 管理和监控 发布订阅 长时间运行的流程 定制 版本控制 常见问题解答 样品 开始 概述 NServiceBus一步一步向导 架构原则 事务 ...
- av_interleaved_write_frame 网络不好的情况下返回较慢
用libvlc做直播推流引擎在网络较差的情况下,需要关闭直播,并且重新开播.这个过程中,推流引擎重启,需要的是快速响应.实际上测试结果发现,经常会发生引擎关闭接口卡住.后来跟踪代码,定位到s_rtmp ...
- 【转】Nginx系列(二)--模块化
原博文出于: http://blog.csdn.net/liutengteng130/article/details/46700977 感谢! 高度模块化的设计设Nginx架构的基础.在Nginx中 ...
- 应用反射写的tostring方法
应用反射写的tostring方法 应用反射写的tostring方法,方便以后查询 代码 package com.chzhao.reflecttest; import java.lang.reflect ...
- eclipse svn切换账号登陆问题
1.当一个人有权限访问文件代码,而另一个账号无法访问该文件代码,要在eclipse上切换账号登陆有权限的账号时,eclipse会用缓存的账号,不会弹出从新输入新账号的窗口. 这样该怎么解决呢? 关闭e ...
- HDU 3577 Fast Arrangement (线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577 题意不好理解,给你数字k表示这里车最多同时坐k个人,然后有q个询问,每个询问是每个人的上车和下车 ...