So I have a use case where I have a ListBuffer of Strings [let's call it 'A'], and an array of Strings (or if it's easier let's take it as another ListBuffer) [let's call it 'B']. There are no duplicate of items in either A or B
Now I want to do this:
if (A.isEmpty){
// do something
}
else if (every item of 'A' must be present in 'B'){
// do something else
}
else{
// do something different
}
This should take care of edge cases like:
- B is empty ['else if' check should fail]
- len(A) < len(B), but items of A are there in B ['else if' check should pass]
- others...
How should I write the else if block in this case?
CodePudding user response:
Slow version
A.forall(B.contains)
Faster version
val bSet = B.to(Set)
A.forall(bSet.contains)
And a version from @jwvh that is probably somewhere in the middle
a.diff(b).isEmpty