27

In java do we have any method to find that a particular string is part of string array. I can do in a loop which I would like to avoid.

e.g.

String [] array = {"AA","BB","CC" };
string x = "BB"

I would like a

if (some condition to tell whether x is part of array) {
      do something
   } else {
     do something else
   }
2
  • Do you want to avoid the loop because you think it's ugly? Or because you want a way to do it that performs better than O(n)? Commented Jul 12, 2016 at 16:28
  • 1
    Just for the look of the code. I know any method will intern do the loop but hides it from user. Commented Jul 12, 2016 at 16:32

3 Answers 3

60

Do something like:

Arrays.asList(array).contains(x);

since that return true if the String x is present in the array (now converted into a list...)

Example:

if(Arrays.asList(myArray).contains(x)){
    // is present ... :)
}

since Java8 there is a way using streams to find that:

boolean found = Arrays.stream(myArray).anyMatch(x::equals);
if(found){
    // is present ... :)
}
Sign up to request clarification or add additional context in comments.

4 Comments

I think it's important to note that this doesn't do away with checking each element in the array to see if it matches x, it just hides that work from the programmer. Not a knock on this answer. People should be aware, that's all.
And consider this creates an ArrayList object just to search for a string.
This will have a performance hit - if you worry about performance
This solution is raising Critical code smell in SonarQube as "This call to 'contains()' may be a performance hot spot if the collection is large."
11

You could also use the commons-lang library from Apache which provides the much appreciated method contains.

import org.apache.commons.lang.ArrayUtils;

public class CommonsLangContainsDemo {

    public static void execute(String[] strings, String searchString) {
        if (ArrayUtils.contains(strings, searchString)) {
            System.out.println("contains.");
        } else {
            System.out.println("does not contain.");
        }
    }

    public static void main(String[] args) {
        execute(new String[] { "AA","BB","CC" }, "BB");
    }

}

Comments

4

This code will work for you:

bool count = false;
for(int i = 0; i < array.length; i++)
{
    if(array[i].equals(x))
    {
        count = true;
        break;
    }
}
if(count)
{
    //do some other thing
}
else
{
    //do some other thing
}

9 Comments

This will not work. you have to set a boolean or count inside loop and then outside the loop i can check that variable to do something. But i want to avoid loop.
The OP wants to do something else if the value doesn't exist in the array, not once per element in the array that isn't the value.
Then you can use a variable initialized with 0 in the loop and see if it matches with any string. And check outside the loop if variable is not 0 then do something else...
Then the answer should reflect that, @VatsalSura. You could edit it, perhaps?
if (count == true) // Redundant, if (count)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.