46

In an if statement in Java how can I check whether an object exists in a set of items. E.g. In this scenario i need to validate that the fruit will be an apple, orange or banana.

if (fruitname in ["APPLE", "ORANGES", "GRAPES"]) {
    //Do something
}

It's a very trivial thing but I couldn't figure out a short and concise way to accomplish this.

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382

4 Answers4

69
static final List<String> fruits = Arrays.asList("APPLE", "ORANGES", "GRAPES");

if (fruits.contains(fruitname))

If your list was much larger, a set would be more efficient.

static final Set<String> fruits = new HashSet<String>(
       Arrays.asList("APPLE", "ORANGES", "GRAPES", /*many more*/));
T04435
  • 12,507
  • 5
  • 54
  • 54
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Might as well make it a `Set`. Seems more appropriate. – wds Jan 14 '11 at 08:53
  • @wds There isn't really an Arrays.asSet method, and if this is the only use for the collection the extra conversion (to Set) step wouldn't seem worth the effort. – extraneon Jan 14 '11 at 10:02
  • Not for a list with three entries. For more entries it might be worth it. – Peter Lawrey Jan 14 '11 at 14:53
  • I'm guessing having to hash the key actually takes longer than doing a lookup in a table of a few dozen elements. Still, `Set` is the right type for the job, even if the particular implementation might not be. – wds Jan 17 '11 at 09:08
  • @wds, true, for a small number of entries, it hardly matters which is faster. You can scan a List of 100K entries in about a milli-second which might be fast enough. – Peter Lawrey Jan 17 '11 at 10:51
  • @PeterLawrey If the list is not a collection of Strings, lets say just a custom object, do we need to override equal method? – WowBow May 29 '12 at 19:50
  • @WowBow It depends on the class, however you may need to override equals/hashCode or compareTo depending on the collection used. – Peter Lawrey May 29 '12 at 20:03
9

for completeness using google-collections/guava:

import com.google.common.collect.Sets;

static final Set<String> fruit = Sets.newHashSet("APPLE", "ORANGES", "GRAPES");

if (fruit.contains(fruitname))

or using the plane old jdk classes:

static final Set<String> fruit = new HashSet<String>(Arrays.asList("APPLE", "ORANGES", "GRAPES"));
Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
2

Is Arrays.binarySearch what you are looking for?

String [] fruits = new String[]{"APPLE", "ORANGES", "GRAPES"};
Arrays.sort(fruits); // binarySearch requires that the array is sorted

if (Arrays.binarySearch(fruits), fruitname) >= 0) {
  // found!
}

And of course the trusted Apache Commons ArrayUtils:

if (ArrayUtils.contains(new String[]{"APPLE", "ORANGES", "GRAPES"}, fruitname){
  // found
}

I knew there would be something in Apache Commons :)

Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
extraneon
  • 23,575
  • 2
  • 47
  • 51
1

If you have Set, List, Map of fruits which all have the same parent: Collection, you can try this example.

String fruitName = "Orange";
Collection<String> fruits = ... // set of fruits
if (fruits.contains(fruitName)) {
    ...
}

(For Java 8/9/10 ways of creating literal Set please see this SO answer.)

Be careful with case sensitivity (Orange != orange).

Greg Dubicki
  • 5,983
  • 3
  • 55
  • 68
lukastymo
  • 26,145
  • 14
  • 53
  • 66