软件编程
位置:首页>> 软件编程>> java编程>> Java中高效判断数组中是否包含某个元素的几种方法

Java中高效判断数组中是否包含某个元素的几种方法

作者:hollis  发布时间:2022-02-21 05:46:39 

标签:Java,包含,元素,数组查找

如何检查一个数组(无序)是否包含一个特定的值?这是一个在Java中经常用到的并且非常有用的操作。同时,这个问题在Stack Overflow中也是一个非常热门的问题。在投票比较高的几个答案中给出了几种不同的方法,但是他们的时间复杂度也是各不相同的。本文将分析几种常见用法及其时间成本。

检查数组是否包含某个值的方法

使用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()

Arrays.binarySearch()方法只能用于有序数组!!!如果数组无序的话得到的结果就会很奇怪。

查找有序数组中是否包含某个值的用法如下:


public static boolean useArraysBinarySearch(String[] arr, String targetValue) {
   int a =  Arrays.binarySearch(arr, targetValue);
   if(a > 0)
       return true;
   else
       return false;
}

时间复杂度

下面的代码可以大概的得出各种方法的时间成本。基本思想就是从数组中查找某个值,数组的大小分别是5、1k、10k。这种方法得到的结果可能并不精确,但是是最简单清晰的方式。


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

使用一个长度为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

使用一个长度为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


String inputStrs = "北京,天津,上海,四川,湖南,广州,深圳,海南";
String[] strList = inputStrs.split(",");

String target = "上海";//1
String result = Arrays.asList(strList).contains(target) ? "查到咯!" : "没这个城市啊...";
//2Set<String> set = new HashSet<>(Arrays.asList(strList));
System.out.println(set.contains(target)); // true
System.out.println(result); // result="查到咯!"-----------

来源:http://www.hollischuang.com/archives/1269

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com