I'd like to find an elegant way to this:
I have two collections A and B, if they both are not empty, then I need to do the intersection (store common elements in another list).
If one of them is empty I'll have to take all elements of the other.
If both are empty the resulting collection will be empty.
Is there a way to solve this problem without using many if conditions and with a good performance?
This is working but it's not so nice:
import org.apache.commons.collections4.CollectionUtils;
...
...
List<Long> listA = new ArrayList<Long>();
List<Long> listB = new ArrayList<Long>();
//initialisation list A & listB
List<Long> outputList = null;
if(listA.size()>0 && listB.size() >0) {
outputList = new ArrayList(CollectionUtils.intersection(listB, listA));
}
else if(listA.size()==0){
outputList = listB;
}
else if(listB.size()==0){
outputList = listA;
}
Thank you!