How To Find Maximum Value from ArrayList in Java - Collections.max()

1. Introduction


In this article, We'll learn how to find the maximum (max) value from ArrayList. Finding the max value from ArrayList from Collection API is done by running a loop over all the elements or can be found max value with the Collections.max() method.

Collections.max(): Returns the maximum element of the given collection, according to the natural ordering of its elements.

Example programs to find the max value from a list of wrapper objects and a list of custom or user-defined objects.


2. Finding the max integer value from ArrayList


Create a list of integer values and pass the list to Collections.max(list). max() method sort the list of values in the natural order and return max value from it.

package com.java.w3schools.blog.arraylist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
*
* Finding max value from list.
*
* @author javaprogramto.com
*
*/
public class ArrayListMaxValue {

public static void main(String[] args) {

// creating list.
List<Integer> intValues = new ArrayList<>();

// adding values
intValues.add(6);
intValues.add(64);
intValues.add(45);
intValues.add(60);
intValues.add(9);
intValues.add(2);

// calling max() method.
Integer max = Collections.max(intValues);
System.out.println("ArrayList values : " + intValues);
System.out.println("ArrayList max value : " + max);
}
}

Output:

ArrayList values : [6, 64, 45, 60, 9, 2]
ArrayList max value : 64

3. Custom Objects - Find Max Salary Employee from list of Employee Objects



  • First create an employee class with id, age and salary fields.
  • Next, create a list of employee objects.
  • Last, Call Collections.sort(listOfEmps) method that should return the Employee object with max salary.


package com.java.w3schools.blog.arraylist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
*
* Find employee object with max salary.
*
* @author javaprogramto.com
*
*/
public class ArrayListCustomObjectMaxValue {

public static void main(String[] args) {

// creating list with type Employee.
List<Employee> empOfList = new ArrayList<>();

// adding values
empOfList.add(new Employee(100, 45, 250000));
empOfList.add(new Employee(109, 38, 15000));
empOfList.add(new Employee(108, 23, 330000));
empOfList.add(new Employee(111, 55, 873983));
empOfList.add(new Employee(103, 25, 908022));
empOfList.add(new Employee(102, 35, 87272));

// calling max() method.
Employee max = Collections.max(empOfList);
System.out.println("ArrayList max value : " + max.getSalary() + " for emp id : " + max.getId());
}
}

class Employee implements Comparable<Employee> {

private int id;
private int age;
private long salary;

public Employee(int id, int age, long salary) {
super();
this.id = id;
this.age = age;
this.salary = salary;
}

public int getId() {
return id;
}

public int getAge() {
return age;
}

public long getSalary() {
return salary;
}

@Override
public int compareTo(Employee o) {
if (this.getSalary() > o.getSalary()) {
return 1;
} else if (this.getSalary() < o.getSalary()) {
return -1;
}
return 0;
}

}


Output:

ArrayList max value : 908022 for emp id : 103

4. Exceptions with Collections.sort()


If the Employee class did not implement a Comparable interface then you will get compile time error as below.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method max(Collection<? extends T>) in the type Collections is not applicable for the arguments (List<Employee>)

at com.java.w3schools.blog.arraylist.ArrayListCustomObjectMaxValue.main(ArrayListCustomObjectMaxValue.java:30)


If the list is having other that Employee Objects then it will throw ClassCastException.

If the list is empty then you will get NoSuchElementException.

5. Conclusion


In this article, we've seen how to find the max element from the list. Examples are shown for wrapper objects and custom objects in the list to find the max value.


GitHub Code 1

GitHub Code 2

Reference API

0 Comments