I have a method to validate no negative numbers in List of numbers:
private void validateNoNegatives(List<String> numbers) {
List<String> negatives = numbers.stream().filter(x->x.startsWith("-")).collect(Collectors.toList());
if (!negatives.isEmpty()) {
throw new RuntimeException("negative values found " + negatives);
}
}
Is it possible to use a method reference instead of x->x.startsWith("-")? I thought about String::startsWith("-") but is not working.
noneMatch. For example,numbers.stream().noneMatch(x -> x.startsWith("-"));