Vii. How to efficiently check whether an array contains a value in Java

Source: Internet
Author: User

How to check whether an array (non-sorted) contains a specific value. This is a very useful or often used in Java. This is a question of high votes in the stack overflow. There are many different ways to deal with the already high-ticket answers, but the complexity of time is very different. Below, I'll show you how much time is spent on each method.

One or four different ways to check that an array contains a specific value

1) Use list

Public Static boolean uselist (string[] arr, String targetvalue) {

return Arrays.aslist (arr). Contains (Targetvalue);

}

2) using Set

Public Static boolean useset (string[] arr, String targetvalue) {

set<string> set = new hashset<string> (Arrays.aslist (arr));

return set.contains (targetvalue);

}

3) with a simple loop

Public Static boolean useloop (string[] arr, String targetvalue) {

for (String S:arr) {

if (S.equals (Targetvalue))

return true;

}

return false;

}

4) with Arrays.binarysearch ()

The following code is wrong and is listed for completeness. BinarySearch () can only be used on sorted arrays. You will feel the result of the following code running is weird.

Public Static boolean usearraysbinarysearch (string[] arr, String targetvalue) {

int a = Arrays.binarysearch (arr, targetvalue);

if (A > 0)

return true;

Else

return false;

}

Second, the complexity of time

The following approximate time measurement. The basic method below is to test to find a quantity of 5, 1k, 10K. This treatment may be imprecise, but it is clear and simple.

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);

}

Results:

Uselist:13

useset:72

Useloop:5

Usearraysbinarysearch:9

With a large Array (1K):

string[] arr = new string[1000];

Random s = new random ();

for (int i=0; i<; i++) {

Arr[i] = string.valueof (S.nextint ());

}

Results:

uselist:112

useset:2055

useloop:99

Usearraybinary:12

With a larger array (10K)

string[] arr = new string[10000];

Random s = new random ();

for (int i=0; i< 10000; i++) {

Arr[i] = string.valueof (S.nextint ());

}

Results:

uselist:1590

useset:23819

useloop:1526

Usearraybinary:12

Obviously, it is more efficient to use a simple looping method than to use any set. Many developers use the first method, but it is not efficient. Passing an array to another collection requires that all elements be transformed before the operation.

If an array is sorted. If the Arrays.binarysearch () method is used.

In this case, because the array is not sorted, it cannot be used.

In fact, if you really need to be efficient in finding whether a value is in an array or collection, a sorted list or tree can be done in O (log (n)) or HashSet at O (1).

Vii. How to efficiently check whether an array contains a value 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.