7

Collections.max(arraylist) doesn't work, and a regular for loop won't work either.

What I have is:

ArrayList<Forecast> forecasts = current.getForecasts();

Collections.max(forecast) gives me this error:

The method max(Collection<? extends T>) in the type Collections is
not applicable for the arguments (ArrayList<Forecast>)

The ArrayList holds Forecast objects which each has an int field for the temperature of each day. I am trying to store the max in an int max.

6
  • 1
    Does Forecast class implement Comparable? Commented Apr 29, 2017 at 23:21
  • 1
    The forecasts ArrayList of Forecast, is not an array of int. Both a loop and max() work correctly when used appropriately. Commented Apr 29, 2017 at 23:21
  • You have to use Compactor interface Commented Apr 29, 2017 at 23:22
  • How does not using a regular for loop work? Can you share the code you tried with? Commented Apr 29, 2017 at 23:23
  • @PeterLawrey could you please expand on that? If forecasts is not an array of integers, how can I get the max integer value in it? Commented Apr 29, 2017 at 23:29

5 Answers 5

21

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();
Sign up to request clarification or add additional context in comments.

1 Comment

works flawlessly!
1

Streams are perfect for these sort of problems.

Forecast highest = forecasts.stream()
                            .max((fc1, fc2) -> fc1.getTemp() - fc2.getTemp())
                            .get();

Comments

1

Another solution is use map reduce:

Optional<Forecast> element = forecasts
                     .stream()
                     .reduce((a,b) -> a.getTemperature() > b.getTemperature() ? a : b );

In this way you could even use use parallelStream()

Comments

0

here is the simple method that return a max value from arraylist

public static object GetMaxValue(ArrayList arrayList)

{

 ArrayList sortArrayList= arrayList;

 sortArrayList.Sort();

 sortArrayList.Reverse();

 return sortArrayList[0];

}

Comments

-2

I have two ideas. First of all, you used forecast in the parameter, but it should be forecasts. Also, you never gave your code for the class Forecast. I may be wrong, because I didn't see all of your code, but you need to convert Forecast to a Integer. Do this by Integer i = Integer.parseInt(forecasts); Next use Collections.max(i)

1 Comment

You can only parse a String, parseInt returns an int, not an Integer and once you have just one value there is no point calling max () as it is the maximum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.