Tag: value target time int equals import val pre get
The most efficient way to operate a Java array is to loop through the values, and if you convert to a collection then you allocate memory, which is less efficient than the former, but there are many methods that need to be weighed against performance tuning. Remember: arrays are arrays, collections are collections.
Here's how to collect the most commonly used arrays to go to a collection:
Importorg.apache.commons.lang3.ArrayUtils;Importjava.util.Arrays;ImportJava.util.HashSet;ImportJava.util.Set;//method to check if an array contains a value Public classTestarray {//using the list Public Static Booleanuselist (string[] arr, String targetvalue) {returnarrays.aslist (arr). Contains (Targetvalue); } //using Set Public Static BooleanUseset (string[] arr, String targetvalue) {Set<String> set =NewHashset<string>(Arrays.aslist (arr)); returnSet.contains (Targetvalue); } //use loop to Judge Public Static BooleanUseloop (string[] arr, String targetvalue) { for(String s:arr) {if(S.equals (targetvalue))return true; } return false; } //find the use of a value in an ordered array Public Static BooleanUsearraysbinarysearch (string[] arr, String targetvalue) {intA =Arrays.binarysearch (arr, targetvalue); if(A > 0) return true; Else return false; } //using Arrayutils Public Static Booleanusearrayutils (string[] arr, String targetvalue) {returnarrayutils.contains (arr, targetvalue); } Public Static voidMain (string[] args) {string[] arr=NewString[] {"CD", "BC", "EF", "DE", "AB", "JK" }; //Use list LongStartTime =System.nanotime (); for(inti = 0; I < 100000; i++) {uselist (arr),A); } LongEndTime =System.nanotime (); LongDuration = EndTime-StartTime; System.out.println ("Uselist:" + duration/1000000); //Use set LongStartTime2 =System.nanotime (); for(inti = 0; I < 100000; i++) {Useset (arr),A); } LongEndTime2 =System.nanotime (); LongDuration2 = endTime2-startTime2; System.out.println ("Useset:" + duration/1000000); //Use loop LongStartTime3 =System.nanotime (); for(inti = 0; I < 100000; i++) {Useloop (arr),A); } LongEndTime3 =System.nanotime (); LongDuration3 = EndTime3-StartTime3; System.out.println ("Useloop:" + duration/1000000); //Use arrays.binarysearch () LongStartTime4 =System.nanotime (); for(inti = 0; I < 100000; i++) {Usearraysbinarysearch (arr),A); } LongEndTime4 =System.nanotime (); LongDuration4 = EndTime4-StartTime4; System.out.println ("Usearraysbinarysearch:" + duration/1000000); }}/** Obviously, using a simple loop method is more efficient than using any set. Many developers use the first method for convenience, but his efficiency is relatively low. * Because the array is pressed into the collection type, the array element is first traversed once, and then the collection class is used for other operations. */
To import the following packages when using:
<!--Https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -<Dependency> <groupId>Org.apache.commons</groupId> <Artifactid>Commons-lang3</Artifactid> <version>3.6</version></Dependency>
Reference:
Http://www.cnblogs.com/ipetergo/p/6429747.html (the above content is transferred from this article)
Http://www.importnew.com/18700.html
Java Array Operation method Collection (quickly judging a value in this array)