In the code below, it takes a stream, sorts it. If there is a maximum limit to be applied, he will apply it.
if(maxLimit > 0) {
return list.stream().sorted(comparator).limit(maxLimit).collect(Collectors.toList());
} else {
return list.stream().sorted(comparator).collect(Collectors.toList());
}
Here, inside if, is there a limit operation, and otherwise it does not exist. Other operations in the stream are the same.
Is there a way to apply the limit when maxLimit is greater than zero. In the above code block, the same logic is repeated, with the exception of the restriction operation in one block.
source
share