博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
四种方法校验数组中是否包含某个指定的字符串
阅读量:6533 次
发布时间:2019-06-24

本文共 3810 字,大约阅读时间需要 12 分钟。

校验数组中是否包含某个指定的字符串这个场景会经常使用到,有一次无意中看到一篇文章,觉得写得挺好,扣出来给大家分享下,先说明下,是从一个老外的网站上弄过来的。

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, this can be done in several different ways, but the time complexity could be very different. In the following I will show the time cost of each method.

 

1. Four Different Ways to Check If an Array Contains a Value

1) Using List:

public static boolean useList(String[] arr, String targetValue) {	return Arrays.asList(arr).contains(targetValue);}

2) Using Set:

public static boolean useSet(String[] arr, String targetValue) {	Set
set = new HashSet
(Arrays.asList(arr)); return set.contains(targetValue);}

3) Using a simple loop:

public static boolean useLoop(String[] arr, String targetValue) {	for(String s: arr){		if(s.equals(targetValue))			return true;	}	return false;}

4) Using Arrays.binarySearch():

* The code below is wrong, it is listed here for completeness. binarySearch() can ONLY be used on sorted arrays. You will see the result is weird when running the code below.

public static boolean useArraysBinarySearch(String[] arr, String targetValue) {		int a =  Arrays.binarySearch(arr, targetValue);	if(a > 0)		return true;	else		return false;}

2. Time Complexity

The approximate time cost can be measured by using the following code. The basic idea is to search an array of size 5, 1k, 10k. The approach may not be precise, but the idea is clear and simple.

public static void main(String[] args) {	String[] arr = new String[] {  "CD",  "BC", "EF", "DE", "AB"}; 	//use list	long startTime = System.nanoTime();	for (int i = 0; i < 100000; i++) {		useList(arr, "A");	}	long endTime = System.nanoTime();	long duration = endTime - startTime;	System.out.println("useList:  " + duration / 1000000); 	//use set	startTime = System.nanoTime();	for (int i = 0; i < 100000; i++) {		useSet(arr, "A");	}	endTime = System.nanoTime();	duration = endTime - startTime;	System.out.println("useSet:  " + duration / 1000000); 	//use loop	startTime = System.nanoTime();	for (int i = 0; i < 100000; i++) {		useLoop(arr, "A");	}	endTime = System.nanoTime();	duration = endTime - startTime;	System.out.println("useLoop:  " + duration / 1000000); 	//use Arrays.binarySearch()	startTime = System.nanoTime();	for (int i = 0; i < 100000; i++) {		useArraysBinarySearch(arr, "A");	}	endTime = System.nanoTime();	duration = endTime - startTime;	System.out.println("useArrayBinary:  " + duration / 1000000);}

Result:

useList:  13useSet:  72useLoop:  5useArraysBinarySearch:  9

Use a larger array (1k):

String[] arr = new String[1000]; Random s = new Random();for(int i=0; i< 1000; i++){	arr[i] = String.valueOf(s.nextInt());}

Result:

useList:  112useSet:  2055useLoop:  99useArrayBinary:  12

Use a larger array (10k):

String[] arr = new String[10000]; Random s = new Random();for(int i=0; i< 10000; i++){	arr[i] = String.valueOf(s.nextInt());}

Result:

useList:  1590useSet:  23819useLoop:  1526useArrayBinary:  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 requires 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).

原文:http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/

转载地址:http://zwzdo.baihongyu.com/

你可能感兴趣的文章
SilverLigth学习笔记--控制 Silverlight控件样式(转)
查看>>
poj3262
查看>>
第四十天笔记
查看>>
4、动态代理
查看>>
Loj #6073.「2017 山东一轮集训 Day5」距离
查看>>
我的TCP/IP学习笔记
查看>>
shell--字符串的截取变量子串串
查看>>
Cas_个人理解
查看>>
UISearchController
查看>>
梦断代码阅读笔记02
查看>>
轮毂电机光电增量编码器的ABZ信号详解
查看>>
TextBox Template
查看>>
Linux MySQL 储存中文失败简单解决办法
查看>>
洛谷——P1330 封锁阳光大学
查看>>
css选择器
查看>>
zabbix-agent配置文件说明
查看>>
linux系统配置之bash shell的配置(centos)
查看>>
linux C 9*9
查看>>
hdu 1695: GCD 【莫比乌斯反演】
查看>>
python的string操作总结
查看>>