1. 通過將數組轉換成list,然后使用list中的contains進行判斷其是否存在
1
2
3
|
public static boolean uselist(string[] arr,string containvalue){ return arrays.aslist(arr).contains(containvalue); } |
需要注意的是arrays.aslist這個方法中轉換的list并不是java.util.arraylist而是java.util.arrays.arraylist,其中java.util.arrays.arraylist中不能對數組的長度進行擴容操作,這個尤為重要,其中contains實現如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
@override public boolean contains(object o) { //調用indexof方法判斷其在那個位置,判斷其時候為-1 return indexof(o) != - 1 ; } @override public int indexof(object o) { //獲取元素 e[] a = this .a; //判斷空 if (o == null ) { //循環判斷 for ( int i = 0 ; i < a.length; i++) //如果元素為null if (a[i] == null ) //則返回 return i; } else { //如果其不為空 for ( int i = 0 ; i < a.length; i++) //判斷元素與a[i]是否相等 if (o.equals(a[i])) //相等返回i return i; } //否則返回-1 return - 1 ; } |
2. 使用set進行實現判斷是否存在
1
2
3
|
public static boolean useset(string[] arr,string containvalue){ return new hashset<>(arrays.aslist(arr)).contains(containvalue); } |
原理將數組->list->set使用set進行比較
源碼:通過調用map的containskey實現的,而hashmap中則是通過遍歷hash表中的key實現
1
2
3
|
ypublic boolean contains(object o) { return map.containskey(o); } |
3. 使用循環來實現,自己編寫一個循環來判斷
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public static boolean useloop(string[] arr,string containvalue){ //判斷是否為空 if (arr== null ||arr.length== 0 ){ return false ; } for ( int i = 0 ; i < arr.length; i++) { //all null if (containvalue!= null &&containvalue.equals(arr[i])){ return true ; } else if (arr[i]== null ){ return true ; } } return false ; } |
4. 使用org.apache.commons.lang3.arrayutils中的contains方法來實現
1
2
3
|
public static boolean useutils(string[] arr,string containvalue){ return arrayutils.contains(arr,containvalue); } |
具體實現源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public static boolean contains( final object[] array, final object objecttofind) { //調用indexof進行判斷位置 return indexof(array, objecttofind) != index_not_found; } public static int indexof( final object[] array, final object objecttofind, int startindex) { //判斷null if (array == null ) { return index_not_found; } //判斷起始位置 if (startindex < 0 ) { startindex = 0 ; } //判斷查詢元素是否為null if (objecttofind == null ) { //null則直接使用==進行循環判斷位置 for ( int i = startindex; i < array.length; i++) { if (array[i] == null ) { return i; } } //判斷元素是不是array中的元素的實例,如果是則循環并采用equals進行判斷 } else if (array.getclass().getcomponenttype().isinstance(objecttofind)) { for ( int i = startindex; i < array.length; i++) { if (objecttofind.equals(array[i])) { return i; } } } //返回沒有找到 return index_not_found; } |
使用循環1w次來檢測效率
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public static void recompilearr(string[] arr,string containvalue){ //using list long start = system.nanotime(); for ( int i = 0 ; i < 10000 ; i++) { uselist(arr,containvalue); } long end=system.nanotime(); system.out.println( "using list->" +(end-start)/ 10000 ); //using set start = system.nanotime(); for ( int i = 0 ; i < 10000 ; i++) { useset(arr,containvalue); } end=system.nanotime(); system.out.println( "using set->" +(end-start)/ 10000 ); //using loop start = system.nanotime(); for ( int i = 0 ; i < 10000 ; i++) { useloop(arr,containvalue); } end=system.nanotime(); system.out.println( "using loop->" +(end-start)/ 10000 ); //using utils start = system.nanotime(); for ( int i = 0 ; i < 10000 ; i++) { useutils(arr,containvalue); } end=system.nanotime(); system.out.println( "using utils->" +(end-start)/ 10000 ); } |
結果如下圖:
using list->973
using set->2676
using loop->448
using utils->1364
使用的jdk版本為jdk1.8.0_172版本,由上面可以推斷出來
以上四種方法的效率高->低
loop>list>utils>set
對比之下,其實可以看出,采用loop方法進行判斷的效率最高,再過去list,再過去utils再過去set
總結:
分析一下慢的原因:
loop最快,直接操作array,毫無疑問
list次之,由于需要創建一個java.util.array.arraylist,創建對象需要時間所以會更慢一些
util第三,由于其雖然使用的和loop差不多,但是array.getclass().getcomponenttype().isinstance(objecttofind),該段代碼采用調用了本地native方法,我們知道,通過調用本地native方法會比直接調用java方法更加耗時。而且查看源碼可知getclass()與getcomponenttype()以及isinstance都是native方法,非常耗時
set最差,由于其先將array轉換成list,再講list轉換成set,在set中又是采用hashmap來實現的,由于其多次轉換對象,自然,效率也肯定好不到哪里去了。
其實我個人還是比較喜歡使用arrayutils進行操作,雖然說相對相率低一點,但是還會不會差很多。
以上所述是小編給大家介紹的詳解java中數組判斷元素存在幾種方式比較,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/lonecloud/p/9290013.html