Enumerating a list to define a specific value

I have a list of huge lists of strings. The strings "ACCEPTED", "OK" or "NOT OK".

If the list contains one ACCEPTED

, the final value should be ACCEPTED

, regardless of other values. If not ACCEPTED

, but there is OK

, the final value should be OK

. If none of these appear, the final value should be NOT OK

.

I can use a method String.contains()

to check for values ​​that contain a specific string, but I have a huge list. So I am afraid it will cause a performance issue.

if(s.contains(("ACCEPTED"))) {
               value= "ACCEPTED";
               break;
            } else if(s.contains(("OK"))) {
                value= "OK";
                break;
            } else {
                value= "NOT OK";
            }

      

Will this method work for huge lists, or do I need to use something else?

+3


source to share


1 answer


It's basically a question if ArrayList.contains()

fast enough. This has already been answered here:

Time complexity ArrayList.contains()

is equal O(n)

. In layman's terms, the performance line is linear. A list with twice as many items will take twice as long to execute. This is almost as good as IMO, you don't have to worry about performance.

But as Tom said, it is better to use Set

which, by definition, does not preserve duplicate values.



EDIT:

Here is a working code example, you can also test it here :

import java.util.*;

class myCode
{
    public static void main (String[] args) throws java.lang.Exception
    {
        ArrayList<String> testList = new ArrayList<String>(); 
        String s1 = new String ("ACCEPTED");
        String s2 = new String ("OK");
        String s3 = new String ("NOT OK");


        testList.add(s1);
        testList.add(s2);
        testList.add(s3);


        String status= "";

        if(testList.contains("ACCEPTED")) {
             status= "ACCEPTED";
        }
        else if(testList.contains("OK")) {
            status= "OK";
        } 
          else {
            status= "NOT OK";
         }
        System.out.println(status);

    }
}

      

+1


source







All Articles