Let’s say I have an array of primitives or a list of objects, doesn’t matter, is there a short way of doing this kind of check:
if (a_primitive == any_item_in_my_collection) {
// do something
}
or
if (an_object_ref.equals(any_item_in_my_collection)) {
// do something
}
without doing this
for (int i = 0; i < myArray.length; i++) {
if (a_primitive == myArray[i]) {
// do something
}
}
Thank you!
Do you have to use arrays? Why not switch to
Collection-based API?Then you can use
Collection.contains(Object o).Depending on the implementation, this query can be answered in
O(1)orO(log N).