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初始化完成,以便上线时,能及早发 ...
随机推荐
- Min Edit Distance
Min Edit Distance ----两字符串之间的最小距离 PPT原稿参见Stanford:http://www.stanford.edu/class/cs124/lec/med.pdf Ti ...
- Nuttx操作系统
前几天答辩的时候看到有同学在用,回来后查了点资料. 来源:天又亮了 1 NuttX 实时操作系统 NuttX 是一个实时操作系统(RTOS),强调标准兼容和小型封装,具有从8位到32位微控制器环境的 ...
- J2SE7规范_2013.2_类
8.1 类的定义 包括普通类和枚举类,枚举(略) 下面都是指普通类: public只能用于外部类,成员类,不能用于局部类,匿名类 protected和private用于成员类时(待解) sta ...
- 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇:简介及目录》(附上完整工程文件)
G-3D引擎2D射击类游戏制作教程 游戏类型: 打飞机游戏属于射击类游戏中的一种,可以划分为卷轴射击类游戏. 视觉表现类型为:2D 框架简介: Genesis-3D引擎不仅为开发者提供一个3D游戏制作 ...
- FindBugs Bug Descriptions
FindBugs Bug Descriptions ◇例1: Integer a = ; String str ="; System.out.println(str == a.toStrin ...
- ESXi 与其它虚拟化底层产品之比较:
序号 虚拟化管理程序属性 VMware ESXi 5.0 采用 Hyper-V 的 Windows Server 2008 R2 SP1 Citrix XenServer 5.6 FP1 1 磁盘占 ...
- Java 编程要点之并发(Concurrency)详解
计算机用户想当然地认为他们的系统在一个时间可以做多件事.他们认为,他们可以工作在一个字处理器,而其他应用程序在下载文件,管理打印队列和音频流.即使是单一的应用程序通常也是被期望在一个时间来做多件事.例 ...
- MVC使用Google OAuth[OWIN]注意事項
1.前提條件,申請一個client id,頁面:https://console.developers.google.com/ 2.添加連接域名,javascript那欄位為域名即可,另一欄需要加上具體 ...
- Linux命令(1)-scp
Linux scp命令用于Linux之间复制文件和目录,包括从本地复制到远程.从远程复制到本地是两种使用方式. 命令基本格式 scp [可选参数] file_source file_target 从本 ...
- uniPanel特效
function beforeInit(sender){ sender.layout='accordion'; Ext.apply (sender, {title:'供应链',style:'text- ...