I have some Java code that provides objects from items. It limits them based on the maxNumber:
items.stream()
.map(this::myMapper)
.filter(item -> item != null)
.limit(maxNumber)
.collect(Collectors.toList());
It works properly, but the question is this: Is there a way of skipping the limiting when the maxNumber == 0?
I know I could do this:
if (maxNumber == 0) {
items.stream()
.map(this::myMapper)
.filter(item -> item != null)
.collect(Collectors.toList());
} else {
items.stream()
.map(this::myMapper)
.filter(item -> item != null)
.limit(maxNumber)
.collect(Collectors.toList());
}
But perhaps there's a better way, does anything come to your mind?