Comparison of several ways in which an array contains an element is judged in Java

Source: Internet
Author: User

Idle, compare the speed of several ways in which an array contains an element in Java, directly on the code

Talk was cheap, show you the code

Package Test.contain.lishaojie;

Import Java.util.Arrays;
Import Java.util.HashSet;
Import Java.util.Set;

public class Testcontain {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
string[] arr = new string[] {"DD", "CC", "DD", "FF", "KK"};
String target = "A";
int times = 1000;//
Convert to list mode
Long startTime = System.currenttimemillis ();
for (int i = 0; I < times; i++) {
Bylist (arr, target);
}
Long endTime = System.currenttimemillis ();
Long Duration = Endtime-starttime;
SYSTEM.OUT.PRINTLN ("list mode:" + duration/1000000);

Convert to set mode
StartTime = System.currenttimemillis ();
for (int i = 0; I < times; i++) {
Byset (arr, target);
}
EndTime = System.currenttimemillis ();
Duration = Endtime-starttime;
SYSTEM.OUT.PRINTLN ("Set mode:" + duration/1000000);

Direct loop mode
StartTime = System.currenttimemillis ();
for (int i = 0; I < times; i++) {
Byforloop (arr, target);
}
EndTime = System.currenttimemillis ();
Duration = Endtime-starttime;
System.out.println ("Loop mode:" + duration/1000000);

Binary Method Search
StartTime = System.currenttimemillis ();
for (int i = 0; I < times; i++) {
Byarraysbinarysearch (arr, target);
}
EndTime = System.currenttimemillis ();
Duration = Endtime-starttime;
System.out.println ("Two-way Search:" + duration/1000000);

}
public static Boolean bylist (string[] arr, String targetvalue) {
return Arrays.aslist (arr). Contains (Targetvalue);
}
public static Boolean Byset (string[] arr, String targetvalue) {
set<string> set = new Hashset<string> (Arrays.aslist (arr));
Return Set.contains (Targetvalue);
}
public static Boolean Byforloop (string[] arr, String targetvalue) {
for (String S:arr) {
if (S.equals (Targetvalue))
return true;
}
return false;
}
public static Boolean Byarraysbinarysearch (string[] arr, String targetvalue) {
int a = Arrays.binarysearch (arr, targetvalue);
if (a > 0)
return true;
Else
return false;
}

}

The results of the operation are as follows:

List mode: 5
Set Mode: 22
Circulation Mode: 2
Dichotomy Search: 3

After a lot of data to test the cycle of the most efficient, followed by the dichotomy, and finally the list, and set because because the array is pressed into the collection type, the array elements are first traversed, and then use the collection class to do other operations. But the list way is obviously much faster than the Set method, which is why? Look directly at the code: first

@SafeVarargs
@SuppressWarnings ("VarArgs")
public static <T> list<t> aslist (T ... a) {
return new Arraylist<> (a);
}

The return is ArrayList so the set mode also takes one operation to convert ArrayList to set,

Public HashSet (collection<? extends e> c) {
Map = new Hashmap<> (Math.max ((int) (C.size ()/.75f) + 1, 16));
AddAll (c);
}

One of the AddAll methods:

public boolean addall (collection<? extends e> c) {
Boolean modified = false;
for (E e:c)
if (Add (e))
Modified = true;
return modified;
}

Once again the selection ring, so the efficiency is lower, binggo

Comparison of several ways in which an array contains an element is judged in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.