How do I check if an array (unsorted) contains a specific value? In Java, this is a very useful and common operation. At the same time, in StackOverflow, sometimes a problem with a very high ticket. In several responses to the higher votes, the complexity of time varies greatly.
1. Different ways of realization
Using the list
1 Public Static Boolean uselist (string[] arr, String targetvalue) {2 return arrays.aslist (arr). Contains (targetvalue); 3 }
Using Set
1 Public Static Boolean Useset (string[] arr, String targetvalue) {2 New Hashset<string>(Arrays.aslist (arr)); 3 return Set.contains (targetvalue); 4 }
Using loops
1 Public Static BooleanUseloop (string[] arr, String targetvalue) {2 for(String s:arr) {3 if(S.equals (Targetvalue)) {4 return true;5 }6 }7 return false;8}
Using Arrays.binarysearch
This method is a two-point search method, so you need to use the sort () method to sort the array before querying, if the array is not sorted, the result is indeterminate and the other
If the array contains more than one element of the specified value, it is not guaranteed which one is found.
1 Public Static BooleanUsearraysbinarysearch (string[] arr, String targetvalue) {2 intA =Arrays.binarysearch (arr, targetvalue);3 if(A > 0) {4 return true;5}Else {6 return false;7 }8}
2. Complexity of Time
Use the following code to roughly compare the time complexity between different implementations. Although not very precise, but the idea is really correct. We'll look at the different expressions of the array in the case of 5, 1k, and 10k elements.
5 x
1 Public Static voidMain (string[] args) {2string[] arr =Newstring[]{"CD", "BC", "EF", "DE", "AB"};3 4 //Use list5 LongStartTime =system.nanotime ();6 for(inti = 0; I < 100000; i++) {7Uselist (arr, "A");8 }9 LongEndTime =system.nanotime ();Ten LongDuration = EndTime-StartTime; OneSystem.out.println ("uselist:" + duration/1000000); A - //Use set -StartTime =system.nanotime (); the for(inti = 0; I < 100000; i++) { -Useset (arr, "A"); - } -EndTime =system.nanotime (); +Duration = EndTime-StartTime; -System.out.println ("Useset:" + duration/1000000); + A //Use loop atStartTime =system.nanotime (); - for(inti = 0; I < 100000; i++) { -Useloop (arr, "A"); - } -EndTime =system.nanotime (); -Duration = EndTime-StartTime; inSystem.out.println ("Useloop:" + duration/1000000); - to //Use Arrays. BinarySearch () +StartTime =system.nanotime (); - for(inti = 0; I < 100000; i++) { theUsearraysbinarysearch (arr, "A"); * } $EndTime =system.nanotime ();Panax NotoginsengDuration = EndTime-StartTime; -System.out.println ("usearraybinary:" + duration/1000000); the}
Results
Uselist: useset :useloop : 2usearraybinary :7
1k elements
1 int length = +; 2 New String[length]; 3 4 New Random (); 5 for (int i = 0; i < length; i++) {6 arr[i] = string.valueof (S.nextint ()); 7 }
Results
Uselist: useset :useloop : $usearraybinary :9
10k elements
1 int length = 10000; 2 New String[length]; 3 4 New Random (); 5 for (int i = 0; i < length; i++) {6 arr[i] = string.valueof (S.nextint ()); 7 }
Results
Uselist: 1678useset: 25609useloop: 1802usearraybinary: 10
It is clear from the results above that using a simple loop is more efficient than using other collection operations. Many developers use the first method, but it is not the most efficient. Converting an array to any other collection type requires that all elements be read into the collection class before other things can be done on the collection type.
When you use a Arrays.binarySearch()
method, the array must be ordered. You cannot use this method if the array is not well-ordered.
In fact, if you really need to efficiently check whether an array or a set contains a value, a sorted array or tree can achieve a O(log(n))
time complexity, HashSet
or even a O(1)
time complexity
Transfer from http://www.diguage.com/archives/112.html
Java efficiently checks if an array contains a value