I'm getting this exception when trying to peek an object from an ArrayList
based on the min value of an attribute.
Here is a snapchatCode from my program:
return individuals.stream()
.parallel()
.filter(s -> s.getFitness() != fittest.getFitness())
.min(Comparator.comparingInt(object -> object.getFitness()))
.get();
individuals is an ArrayList<Individual>
.
class Individual{
int fitness;
public int getFitness() {
return fitness;
}
}
The exception i'm getting on the get()
method:
Exception in thread "main" java.util.NoSuchElementException: No value
present
After debugging my program i found that all the individuals of the stream have the same fitness value, so after the filter operation no individual is found, so hen we call the get()
we have an exception
.
How can I manage to get a random value in the case where all the individual have the same fitness value.