How to Check if an Array Contains a Value in Java Efficiently?---reference
2014-04-09 08:39
381 查看
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:
2) Using Set:
3) Using a simple loop:
4) Using Arrays.binarySearech():
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.
Result:
Use a larger array (1k):
Result:
Use a larger array (10k):
Result:
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/
1. 4 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); } |
public static boolean useSet(String[] arr, String targetValue) { Set<String> set = new HashSet<String>(Arrays.asList(arr)); return set.contains(targetValue); } |
public static boolean useLoop(String[] arr, String targetValue) { for(String s: arr){ if(s.equals(targetValue)) return true; } return false; } |
public static boolean useArraysBinarySearch(String[] arr, String targetValue) { int a = Arrays.binarySearch(arr, targetValue); if(a > 0) return true; else return false; } |
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) { 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); } |
useList: 13 useSet: 72 useLoop: 5 useArraysBinarySearch: 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()); } |
useList: 112 useSet: 2055 useLoop: 99 useArrayBinary: 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()); } |
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?
- How to Check if an Array Contains a Value in Java Efficiently?
- How to check if a Number is Positive or Negative in Java - Interview Question
- grails - How to check if element in groovy array/hash/collection/list? - Stack Overflow
- How do you check if a variable is an array in JavaScript? [duplicate]
- 009:How can I test if an array contains a certain value?
- [Groovy] How to check if element in groovy array/hash/collection/list?
- Checking if an array contains a value in C#
- how to increase an regular array length in java?
- In Java, how can I test if an Array contains a certain value?
- Ten Ways to Check if an Integer Is a Power Of Two in C
- How can I check if one string contains another substring in JavaScript?
- Err:The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array
- How to scroll an image, if it does not fit in the display
- Mac环境下mysql初始化密码问题--If you lose this password, please consult the section How to Reset the Root Password in the MySQL reference manual.
- How to combine the value of multiple hashes within an array by the same key
- How to sort an NSMutableArray with custom objects in it?
- How to group anagrams in a string into an array
- how to remove nil and blank string in an array in Ruby
- How to define an array in c#.