如何检查一个数组(无序)是否包含一个特定的值?这是一个在Java中常常用到的而且很是有用的操做。同时,这个问题在Stack Overflow中也是一个很是热门的问题。在投票比较高的几个答案中给出了几种不一样的方法,可是他们的时间复杂度也是各不相同的。本文将分析几种常见用法及其时间成本。html
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; }else{ return false; } } }
Arrays.binarySearch()方法只能用于有序数组!!!若是数组无序的话获得的结果就会很奇怪。
public static boolean useArraysBinarySearch(String[] arr, String targetValue) { int a = Arrays.binarySearch(arr, targetValue); if(a > 0){ return true; }else{ return false; } }
下面的代码能够大概的得出各类方法的时间成本。基本思想就是从数组中查找某个值,数组的大小分别是五、1k、10k。这种方法获得的结果可能并不精确,可是是最简单清晰的方式。java
public class Test01 { 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; } } public static void run1(){ String[] arr = {"CD","BC","CD","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("useArraysBinarySearch:" + duration / 1000000); } public static void main(String[] args) { run1(); } }
结果:数组
useList:4 useSet:34 useLoop:2 useArraysBinarySearch:2
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
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
显然,使用一个简单的循环方法比使用任何集合都更加高效。许多开发人员为了方便,都使用第一种方法,可是他的效率也相对较低。由于将数组压入Collection类型中,首先要将数组元素遍历一遍,而后再使用集合类作其余操做。dom
若是使用Arrays.binarySearch()方法,数组必须是已排序的。因为上面的数组并无进行排序,因此该方法不可以使用。oop
实际上,若是你须要借助数组或者集合类高效地检查数组中是否包含特定值,一个已排序的列表或树能够作到时间复杂度为O(log(n)),hashset能够达到O(1)。code
做者:Hollis_Chuang 来源:http://www.hollischuang.com/archives/1269