I want to check if an item
exists in an item set
.
I want to do this in java:
def is_item_in_set(item, item_set):
return item in item_set
I've managed writing this:
boolean isItemInSet(String item, String[] itemSet) {
for(int i =0; i < itemSet.length; ++i) {
if(item.equals(itemSet[i])) {
return true;
}
}
return false;
}
Is there a better way for testing set-membership in Java?
.contains()
? I've replaced it with.equals()
. – Juglandaceousreturn item in item_set
? They are also not the same as the latter example is not as efficient, it's O(n) while the Python example is O(1). – Bogietrue
andfalse
are not value in Python unless you define them yourself ;) – OvovitellinTrue
. – Bogieitem_set
! – Ovovitellinx_set
it should be a set. Of course, you are right, it could well not be. – Bogie