Java中高效判断数组中是否包含某个元素。

如何检查一个数组(无序的)是否包含一个特定的值呢?这是一个在Java中经常用到的并且非常有用的操作。本文将分析几种常见用法及其时间成本。
1、使用 ava.util.Arrays.asList(arr).contains(target);	
	public static boolean useArrays(String[] arr, String target) {	
		return Arrays.asList(arr).contains(target);	
	}
 

	2、使用java.util.Set<>().contains()
	public static boolean useArrays(String[] arr, String target) {
		return Arrays.asList(arr).contains(target);
	}
	3、使用普通for循环遍历
	public static boolean useLoop(String[] arr, String target) {
		for (int index = arr.length - 1; index < arr.length; --index) {
			if (target.equals(arr[index])) {
				return true;
			}
		}
		return false;
	}

 
 
 
 
 
        4、使用增强for循环遍历
	public static boolean useAmplifyLoop(String[] arr, String target) {

		for (String value : arr) {
			if (value.equals(target)) {
				return true;
			}
		}
		return false;
	}

	5、使用ArrayUtils(新jdk中的类,模仿源码写了一个);
	public static int useArrayUtils(String[] arr, String targetValue) {
		int startIndex = -1;
		if (arr == null) {
			return -1;
		} else {
			if (startIndex < 0) {
				startIndex = 0;
			}

			int i;
			if (targetValue == null) {
				for (i = startIndex; i < arr.length; ++i) {
					if (arr[i] == null) {
						return i;
					}
				}
			} else if (arr.getClass().getComponentType()
					.isInstance(targetValue)) {
				for (i = startIndex; i < arr.length; ++i) {
					if (targetValue.equals(arr[i])) {
						return i;
					}
				}
			}
			return -1;
		}
	}
测试结果如下
public static void main(String[] args) {
		boolean flag = false;
		String[] arr = new String[100000];
		Random s = new Random();
		for (int i = 0; i < arr.length; i++) {
			arr[i] = String.valueOf(s.nextInt(100000));
		}
		String contain = "694";
		arr[arr.length - 1] = contain;
		
		long start = System.nanoTime();
		useArrays(arr, contain);
		ResourceKit.executeNaNoTime(start, "useArrays",flag);

		start = System.nanoTime();
		flag = useSet(arr, contain);
		ResourceKit.executeNaNoTime(start, "useSet",flag);

		start = System.nanoTime();
		flag = useLoop(arr, contain);
		ResourceKit.executeNaNoTime(start, "useLoop",flag);

		start = System.nanoTime();
		flag = useAmplifyLoop(arr, contain);
		ResourceKit.executeNaNoTime(start, "useAmplifyLoop",flag);

		start = System.nanoTime();
		flag = useArrayBinarySearch(arr, contain);
		ResourceKit.executeNaNoTime(start, "useArrayBinarySearch",flag);

		start = System.nanoTime();
		int useArrayUtils = useArrayUtils(arr, contain);
		flag = useArrayUtils < 0 ? false : true;
		ResourceKit.executeNaNoTime(start, "useArrayUtils",flag);
	}

useArrays:23015219   false (该方法好像不能查询?)有空看看源码怎么实现的。
useSet:34215532   true
useLoop:14590   true
useAmplifyLoop:2742914   true
useArrayUtils:2122015   true

目前会的就这几种,测试过程中,倒序for循环是最快的。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值