The Java language is designed to be powerful but also simple. There is no such operator in Java at the language level, but certainly libraries have been written to facilitate such queries.
If you want to know if some object is a member of some set of objects, then instead of an array, you should use -- what else? -- a Set. These data structures naturally allows such queries, and better yet, they're optimized for such queries.
You can easily check if a given string is in a Set<String>
like this:
String[] arr = { "Alice", "Bob", "Carol" };
Set<String> names = new HashSet<String>(Arrays.asList(arr));
System.out.println(names.contains("Alice")); // true
System.out.println(names.contains("Dean")); // false
Using a HashSet
, contains
is a constant-time operation. This is much better than a linear search through an array.
You should familiarize yourself with what data structures are made available for you by the Java Collections Framework. They allow you to write codes that are idiomatic, maintainable, flexible, and supported by many of the powerful algorithms available for these data structures.
See also
- Java Tutorials/Collections Framework
- List<E>, Set<E>, Queue<E>, Map<K,V>, SortedMap<K,V>, NavigableSet<E>, etc.
- Effective Java 2nd Edition, Item 25: Prefer lists to arrays