-1

I want to check if the variable foo is equal to a list of values in java

if( foo == "Cheese" || foo == "a" || foo == "3" ) {
    // ...
}

I want to know how to do something like the following :

String[] values = {"Cheese", "a", "3"};
if( foo in values ) {
    // ...
}

Thank you for helping me !

Alban
  • 31
  • 1
  • 8
  • 2
    `List.contains()`? Or maybe `List.equals()`, depending one what you actually mean by `foo==1` etc. – markspace Oct 24 '17 at 17:58
  • Do you mean like the SQL `IN` keyword? Like `field in (1,2,3)` ? – fvu Oct 24 '17 at 18:02
  • fvu – yes like the IN in SQL and i think there is one in python. markspace – Oh i didn't know List.equals() had this behavior i will try now. cpp beginner – I know the list in advance. – Alban Oct 24 '17 at 18:04
  • No you want Arrays.asList(1, 2, 3).contains(foo) – cpp beginner Oct 24 '17 at 18:06
  • I don't speak Python, so you're going to have to explain in Java what you want. If you thought I was referring to Python methods, I wasn't. That's Java. – markspace Oct 24 '17 at 18:06
  • Don't use an array Use a HashSet. Then use its contains() method. Arrays should generally be avoided in Java. Collections are much more powerful (and safe). – JB Nizet Oct 24 '17 at 18:06
  • contains shows me an error and equals does nothing, my list is a String[] and i want to know if my string is contained in this list – Alban Oct 24 '17 at 18:11
  • Loop through `values` with a for each loop. `for (String i : values)`. Then just use a `if` statement. `if (i.equals(foo))`. `return true` – drewteriyaki Oct 24 '17 at 18:13
  • if (Arrays.asList("1", "2", "3").contains(foo)) does not give a compilation error. – cpp beginner Oct 24 '17 at 18:13
  • *my list is a String[]*: then it's not a list. It's an array. A List and an array are not the same thing. Use collections, not arrays. Google for "Java collections tutorial". – JB Nizet Oct 24 '17 at 18:15
  • Thank you @drewteriyaki, your solution works. JB Nizet i didn't know about that i will follow a tutorial like you said. – Alban Oct 24 '17 at 18:40
  • You should read https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Raedwald Oct 25 '17 at 08:21
  • The title does not match the question body. – Raedwald Oct 25 '17 at 08:22
  • 1
    Possible duplicate of [Compare one String with multiple values in one expression](https://stackoverflow.com/questions/10205437/compare-one-string-with-multiple-values-in-one-expression) – Tom Oct 25 '17 at 21:49

3 Answers3

1

You can do this by using the contains() method of List.

String values[] = {"Cheese", "a", "3"};
String foo = "a";
String bar = "b";
List<String> list = Arrays.asList(values);
System.out.println(list.contains(foo)); // true;
System.out.println(list.contains(bar)); // false;
Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62
0

use contains() in List

    String[] values = {"Cheese", "a", "3"};
    String foo = "3";
    List<String> valuesAsList = Arrays.asList(values);

    boolean containsFoo = valuesAsList.contains(foo);
    System.out.println("foo contained in list " + containsFoo);
LittleBigUllah
  • 313
  • 1
  • 3
  • 9
-4
String[] values = {"Cheese", "a", "3"};
boolean isIn = false;
for (String str : values) {
   if (str == foo) {
      isIn = true;
      break;
   }
}

(Or use str.equals(foo) depending on what you want)

Ltei
  • 425
  • 1
  • 6
  • 14
  • You do know that using `==` to compare strings in Java is not advisable, and that you should use the `equals` method instead, right? – Mark Rotteveel Oct 27 '17 at 19:39