How to find the max element from an array list of objects?
As your ArrayList
contains Forecast
objects you'll need to define how the max
method should find the maximum element within your ArrayList
.
something along the lines of this should work:
ArrayList<Forecast> forecasts = new ArrayList<>();
// Forecast object which has highest temperature
Forecast element = Collections.max(forecasts, Comparator.comparingInt(Forecast::getTemperature));
// retrieve the maximum temperature
int maxTemperature = element.getTemperature();