How to get max element of a list of user defined objects?

The java.util.Collections class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, “wrappers”, which return a new collection backed by a specified collection, and a few other odds and ends.

Collections.max() method will returns the maximum element of the given collection, according to the natural ordering of its elements. Syntax:public static int max(Collection coll)


package com.w3spoint;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Employee implements Comparable<Employee>{    
    private String name;
    private Integer salary;
    public Employee(String name, int salary){ = name;
        this.salary = salary;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public int getSalary() {
        return salary;
    public void setSalary(int salary) {
        this.salary = salary;
    public String toString(){
        return "Name: "", Salary: "+this.salary;
    public int compareTo(Employee emp) {
    	return this.salary.compareTo(emp.getSalary());
public class Test {
        public static void main(String args[]){
        List<Employee> list = new ArrayList<Employee>();
        list.add(new Employee("Jai",50000));
        list.add(new Employee("Mahesh",80000));
        list.add(new Employee("Vishal",60000));
        list.add(new Employee("Hemant",64000));
        Employee employee=Collections.max(list);
        System.out.println("Employee with max salary: "+employee);


Employee with max salary: Name: Mahesh, Salary: 80000

Java Collections class examples

Please follow and like us:
Content Protection by