I have a class where you can optionally specify a Comparator.
Since Comparatorit is optional, I must evaluate its presence and execute the same stream code, either with sorted()or without:
if(comparator != null) {
[...].stream().map()[...].sorted(comparator)[...];
} else {
[...].stream().map()[...];
}
Question:
Is there a more elegant way to do this without duplicating code?
Note:
The default is Comparatornot an option, I just want to keep the original order of the values I pass.
In addition, the elements are already displayed at the sorting point, so I cannot somehow refer to the root list of the stream, since I no longer have the original elements.