1. Method to check whether the array contains a certain value
Use 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 judgment
public static boolean useLoop(String[] arr, String targetValue) { for(String s: arr){ if(s.equals(targetValue)) return true; } return false;}Use Arrays.binarySearch()
Arrays.binarySearch() method can only be used for ordered arrays! ! ! If the array is unordered, the result will be very strange.
The usage of finding whether an ordered array contains a certain value 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;}Time complexity
The following code can roughly draw the time cost of various methods. The basic idea is to find a certain value from an array, and the sizes of the array are 5, 1k, and 10k respectively. The results obtained by this method may not be accurate, but are 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);}Running results:
useList: 13useSet: 72useLoop: 5useArraysBinarySearch: 9
Use an array of length 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 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());}result:
useList: 1590useSet: 23819useLoop: 1526useArrayBinary: 12
summary
Obviously, using a simple loop method is more efficient than using any collection. Many developers use the first method for convenience, but their efficiency is also relatively low. Because you press an array into the Collection type, you must first traverse the array elements and then use the collection class to do other operations.
If you use Arrays.binarySearch() method, the array must be sorted. Since the above array is not sorted, this method is not available.
In fact, if you need to use arrays or collection classes to efficiently check whether the array contains a specific value, a sorted list or tree can achieve a time complexity of O(log(n)), and hashset can achieve O(1).
Using ArrayUtils
In addition to the above, the Apache Commons class library also provides an ArrayUtils class, which can use its contains method to judge the relationship between arrays and values.
import org.apache.commons.lang3.ArrayUtils;public static boolean useArrayUtils(String[] arr, String targetValue) { return ArrayUtils.contains(arr,targetValue);}The same test was performed using arrays of the above lengths, and the result was that the efficiency of this method was between using sets and using loop judgments (sometimes the result was even more ideal than using loops).
useList: 323useSet: 3028useLoop: 141useArrayBinary: 12useArrayUtils: 181----------useList: 3703useSet: 35183useLoop: 3218useArrayBinary: 14useArrayUtils: 3125
In fact, if you look at the source code of ArrayUtils.contains, you can find that it is actually a way to judge whether an element is included in an array.
Some of the codes are 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; }So, in comparison, I prefer to use the ArrayUtils tool class to perform some operations related to the combined number ancestor. After all, he can let me write a lot of code less (because there are inevitably bugs when writing code by myself. After all, the open source tool library provided by apache has been tested by countless developers), and the efficiency is not much lower.
Summarize
OK, the above is all about this article. I hope the content of this article will be of some help to everyone learn or use Java. If you have any questions, you can leave a message to communicate.