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初始化完成,以便上线时,能及早发 ...
随机推荐
- Polymer——Web Components的未来
什么是polymer? polymer由谷歌的Palm webOS团队打造,并在2013 Google I/O大会上推出,旨在实现Web Components,用最少的代码,解除框架间的限制的UI 框 ...
- 1.2……初识Android开发
Android体系结构 Dalvik VM(Android下的java虚拟机)与传统的JVM的区别 传统JVM 基于堆栈的架构 编写.java文件--->编译为.class文件--->打包 ...
- 关于javascript模式一书中var white = new Array(256).join(“ ”)
直接进入正题 var white = new Array(256).join(" ") 运行后,我们会发现white.length的长度是255,这个是为什么呢?书上没有给出解答, ...
- SQL Server 中的三种分页方式
USE tempdb GO SET NOCOUNT ON --创建表结构 IF OBJECT_ID(N'ClassB', N'U') IS NOT NULL DROP TABLE ClassB GO ...
- 【转】iOS开发工具系列(按功能分)
http://www.cocoachina.com/newbie/basic/2014/0417/8187.html 这是我们多篇iOS开发工具系列篇中的一篇,此前的文章比如:那些不能错过的Xcode ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- [iOS UI进阶 - 6.0] CALayer
A.基本知识 1.需要掌握的 CALayer的基本属性 CALayer和UIView的关系 position和anchorPoint的作用 2.概念 在iOS中,你能看得见摸得着的东西基本上都是U ...
- POJ 3922A Simple Stone Game
题目链接 A Sample Stone Game 题目大意:给定n,k,表示最初时有n个石头,两个人玩取石子游戏,第一个人第一次可以取1~n-1个石头,后面每个人最多可以拿走前面一个人拿走的个数的K倍 ...
- 大一上C语言期末大作业-成绩管理系统
都过了半年的作业了,觉得做过去得留下点什么,所以整理了代码发一下博客. 声明:程序在DevC++下用c文件模式可以正常编译使用.(控制台程序) 程序结构:
- 【不积跬步,无以致千里】关闭631端口cups打印服务和8009端口ajp
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...