How do I check if an array (unordered) contains a specific value? This is a very useful operation that is often used in Java. At the same time, this issue is a very hot issue in stack overflow. Several different approaches have been given in the high-voting answers, but their time complexity is varied. This article will analyze several common usages and their time costs.
method to check if an array contains a value
Using the list
public static Boolean uselist (string[] arr, String targetvalue) {
return Arrays.aslist (arr). Contains (Targetvalue);
}
Using Set
public static Boolean Useset (string[] arr, String targetvalue) {
set<string> set = new Hashset<string> (Arrays.aslist (arr));
Return Set.contains (Targetvalue);
}
Use Loop to judge
public static Boolean Useloop (string[] arr, String targetvalue) {
for (String S:arr) {
if (S.equals (Targetvalue))
return true;
}
return false;
}
Using Arrays.binarysearch ()
The Arrays.binarysearch () method can only be used with ordered arrays!!! If the array is unordered, the result will be very strange.
The use of finding whether a value is included in an ordered array is as follows:
public static Boolean Usearraysbinarysearch (string[] arr, String targetvalue) {
int a = Arrays.binarysearch (arr, targetvalue);
if (a > 0)
return true;
Else
return false;
}
Complexity of Time
The following code can approximate the time cost of various methods. The basic idea is to find a value from the array, the size of which is 5, 1k, 10k, respectively. The result of this method may not be accurate, but it is the simplest and clearest way.
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);
}
Operation Result:
Uselist:13
useset:72
Useloop:5
Usearraysbinarysearch:9
Use an array of length 1k
string[] arr = new string[1000];
Random s = new Random ();
for (int i=0; i<; i++) {
Arr[i] = string.valueof (S.nextint ());
}
Results:
uselist:112
useset:2055
useloop:99
Usearraybinary:12
Use an array of length 10k
string[] arr = new string[10000];
Random s = new Random ();
for (int i=0; i< 10000; i++) {
Arr[i] = string.valueof (S.nextint ());
}
Results:
uselist:1590
useset:23819
useloop:1526
Usearraybinary:12
Summarize
Obviously, using a simple loop method is more efficient than using any collection. Many developers use the first method for convenience, but his efficiency is relatively low. Because the array is pressed into the collection type, the array element is first traversed once, and then the collection class is used for other operations.
If you use the Arrays.binarysearch () method, the array must be sorted. Because the array above is not sorted, the method is not available.
In fact, if you need an array or a collection class to efficiently check whether an array contains a specific value, a sorted list or tree can have a time complexity of O (log (n)) and hashset can reach O (1).
(The English text ends, the following is the translator note)
Using Arrayutils
In addition to the above, the Apache Commons Class library also provides a arrayutils class that can use its contains method to determine the relationship between an array and a value.
Import Org.apache.commons.lang3.ArrayUtils;
public static Boolean usearrayutils (string[] arr, String targetvalue) {
Return Arrayutils.contains (Arr,targetvalue);
}
The same is done with arrays of these lengths, and the result is that the efficiency of the method is between the use of the set and the use of cyclic judgment (sometimes the result is even better than the use of loops).
uselist:323
useset:3028
useloop:141
Usearraybinary:12
usearrayutils:181
-------
uselist:3703
useset:35183
useloop:3218
Usearraybinary:14
usearrayutils:3125
In fact, if you look at the source code of Arrayutils.contains can be found, he determines whether an element is included in the array is actually used in the loop to determine the way.
Some of the code is as follows:
if (array = = NULL) {
return-1;
} else {
if (StartIndex < 0) {
StartIndex = 0;
}
int i;
if (Objecttofind = = null) {
for (i = startIndex; i < array.length; ++i) {
if (array[i] = = null) {
return i;
}
}
} else if (Array.getclass (). Getcomponenttype (). Isinstance (Objecttofind)) {
for (i = startIndex; i < array.length; ++i) {
if (Objecttofind.equals (Array[i])) {
return i;
}
}
}
return-1;
}
Turn from: 51496944
So, I prefer to use the Arrayutils tool class to do some composite-related operations, compared to the other. After all, he can let me write a lot less code (because I write code is inevitably bug, after all, Apache provides the open Source Tool class library has been tested by countless developers), and, not too low efficiency.
How to efficiently determine if an element is contained in an array in Java