Java:如何判断一个数组中是否包含某个元素

Java:如何判断一个数组中是否包含某个元素


方法一、使用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<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;
}

方法四、使用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()方法,数组必须是已排序的。所以当数组并没有进行排序,所以该方法不可使用。

  • 30
    点赞
  • 94
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王小二(海阔天空)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值