方法一、使用List
- public static boolean useList(String[] arr, String targetValue) {
-
- return Arrays.asList(arr).contains(targetValue);
-
- }
方法二、使用Set
-
- public static boolean useSet(String[] arr, String targetValue) {
-
- Set
set = new HashSet(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;
-
- }
方法四、使用Arrays.binarySearch()
- public static boolean useArraysBinarySearch(String[] arr, String targetValue) {
-
- int a = Arrays.binarySearch(arr, targetValue);
-
- if(a > 0) {
-
- return true;
-
- }else {
-
- return false;
-
- }
-
- }
注意:Arrays.binarySearch()方法只能用于有序数组!!!
方法五、使用Apache Commons类库中的ArrayUtils
-
- import org.apache.commons.lang3.ArrayUtils;
-
-
- public static boolean useArrayUtils(String[] arr, String targetValue) {
-
- return ArrayUtils.contains(arr,targetValue);
-
- }
其实Apache Commons类库中的ArrayUtils的contains方法的源码也是使用循环判断的方式。
- 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;
-
- }
比较:我们可以通过下面的代码大概的得出各种方法的时间成本。基本思想就是从数组中查找某个值,数组的大小分别是5、1k、10k。这种方法得到的结果可能并不精确,但是是最简单清晰的方式。
- public static void main(String[] args) {
-
- String[] arr = new String[] { "1", "2", "3", "4", "5"};
-
-
-
- /*
- String[] arr = new String[1000];
- for(int i=1;i<=1000;i++) {
- arr[i-1]=String.valueOf(i);
- }
- */
-
-
-
- /*
- String[] arr = new String[10000];
- for(int i=1;i<=10000;i++) {
- arr[i-1]=String.valueOf(i);
- }
- */
-
-
-
- //use list
-
- long startTime = System.nanoTime();
-
- for (int i = 0; i < 100000; i++) {
-
- useList(arr, "5");
-
- }
-
- 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, "5");
-
- }
-
- 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, "5");
-
- }
-
- 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, "5");
-
- }
-
- endTime = System.nanoTime();
-
- duration = endTime - startTime;
-
- System.out.println("useArrayBinary: " + duration / 1000000);
-
-
- //use useArrayUtils
-
- startTime = System.nanoTime();
-
- for (int i = 0; i < 100000; i++) {
-
- useArrayUtils(arr, "5");
-
- }
-
- endTime = System.nanoTime();
-
- duration = endTime - startTime;
-
- System.out.println("useArrayUtils: " + duration / 1000000);
-
- }
当数组长度为5时,运行结果如下:
useList: 4
useSet: 48
useLoop: 3
useArrayBinary: 4
useArrayUtils: 16
当数组长度为1k时,运行结果如下:
useList: 97
useSet: 1312
useLoop: 78
useArrayBinary: 6
useArrayUtils: 97
当数组长度为10k时,运行结果如下:
useList: 1213
useSet: 11697
useLoop: 1165
useArrayBinary: 7
useArrayUtils: 1272
总结:
显然,使用一个简单的循环方法比使用任何集合都更加高效。
大多数人为了方便,都使用第一种方法,但是他的效率相对较低。因为将数组压入Collection类型中,首先要将数组元素遍历一遍,然后再使用集合类做其他操作。
如果使用Arrays.binarySearch()方法,数组必须是已排序的。所以当数组并没有进行排序,所以该方法不可使用。