In Java 8, is there a way to apply the filter on a stream based on a condition,
example
I have this stream
if (isAccessDisplayEnabled) {
src = (List < Source > ) sourceMeta.getAllSources.parallelStream()
.filter(k - > isAccessDisplayEnabled((Source) k))
.filter(k - > containsAll((Source) k, substrings, searchString))
.collect(Collectors.toList());
} else {
src = (List < Source > ) sourceMeta.getAllSources.parallelStream()
.filter(k - > containsAll((Source) k, substrings, searchString))
.collect(Collectors.toList());
}
I am adding the filter
.filter(k - > isAccessDisplayEnabled((Source) k)))
on the stream based on the if-else condition. Is there a way to avoid that if-else, since if there are more filters coming up,then it will be hard to maintain.
Please let me know