26

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
   }
schorsch312
  • 5,553
  • 5
  • 28
  • 57
Hari M
  • 415
  • 1
  • 6
  • 13

3 Answers3

58

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 ... :)
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 7
    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. – Erick G. Hagstrom Jul 12 '16 at 16:29
  • 3
    And consider this creates an ArrayList object just to search for a string. – David Bradley Sep 09 '18 at 13:58
  • 2
    This will have a performance hit - if you worry about performance – avijendr May 05 '20 at 23:56
  • 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." – Haripriya Sep 18 '20 at 15:14
10

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

}
Arthur Noseda
  • 2,534
  • 19
  • 28
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
}
VatsalSura
  • 926
  • 1
  • 11
  • 27
  • 1
    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. – Hari M Jul 12 '16 at 16:23
  • 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. – azurefrog Jul 12 '16 at 16:24
  • 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... – VatsalSura Jul 12 '16 at 16:28
  • Then the answer should reflect that, @VatsalSura. You could edit it, perhaps? – Erick G. Hagstrom Jul 12 '16 at 16:32
  • if (count == true) // Redundant, if (count) – Matthew Smith Jul 12 '16 at 16:35
  • @ErickG.Hagstrom It is done. In-fact I was editing only when u commented. – VatsalSura Jul 12 '16 at 16:35
  • Well done. Now the only problem is that your answer relies on a loop, and OP specifically asked for a method that doesn't use a loop. So even though it will work, it isn't a correct answer to this specific question. – Erick G. Hagstrom Jul 12 '16 at 18:33
  • @ErickG.Hagstrom I know it. I am also new to java and didn't knew the method, so I just posted my logic. But now I know the method to do it without loops as answered below. – VatsalSura Jul 12 '16 at 18:37
  • Well then, welcome to the wonderful world of Java :-) Knowing how to make something work is always the most important thing. – Erick G. Hagstrom Jul 12 '16 at 19:15