Home  >  Core Java

Collections.max in Java

By Arvind Rai, February 06, 2022
Find the Java doc of Collections.max method.
a. Max element is found on the basis of Comparable interface implemented by class of collection element. Pass Collection instance.
static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) 
It returns the maximum element of the given collection on the basis of natural ordering of its elements. The collection must contain the elements of the class implementing Comparable interface. All elements in the collection must be mutually comparable i.e. e1.compareTo(e2) must not throw a ClassCastException.

b. Max element of the collection is found on the basis of specified Comparator instance. Pass Collection and Comparator instances.
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) 
It returns the maximum element of the given collection, according to the order induced by the specified comparator. All elements in the collection must be mutually comparable by the specified comparator. The comp.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the collection.

Example-1

CollectionsMax1.java
package com.concretepage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsMax1 {
  public static void main(String[] args) {
     List<Integer> numList = new ArrayList<>();
     numList.add(10);
     numList.add(30);
     numList.add(20);
     
     int numMax = Collections.max(numList);
     System.out.println(numMax); // Output: 30
     
     List<String> strList = new ArrayList<>();
     strList.add("Mohan");
     strList.add("Ramesh");
     strList.add("Mahesh");
     
     String strMax = Collections.max(strList);
     System.out.println(strMax); // Output: Ramesh     
  }
} 

Example-2

CollectionsMax2.java
package com.concretepage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsMax2 {
  public static void main(String[] args) {
	User u1 = new User("Rohit", 20);
	User u2 = new User("Vivek", 22);
	User u3 = new User("Raju", 25);
	
	List<User> list = new ArrayList<User>();
	list.add(u1);
	list.add(u2);
	list.add(u3);

	User maxUser = Collections.max(list);
	System.out.println(maxUser.name); // Output: Vivek
  }
}

class User implements Comparable<User> {
  String name;
  int age;

  public User(String name, int age) {
	this.name = name;
	this.age = age;
  }

  @Override
  public int compareTo(User o) {
	return name.compareTo(o.name);
  }
} 

Example-3

Here we will pass Comparator instances to Collection to find max element.
CollectionsMax3.java
package com.concretepage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionsMax3 {
  public static void main(String[] args) {
	Person p1 = new Person("Rohit", 20);
	Person p2 = new Person("Vivek", 22);
	Person p3 = new Person("Raju", 25);

	List<Person> list = new ArrayList<Person>();
	list.add(p1);
	list.add(p2);
	list.add(p3);

	Comparator<Person> compByName = Comparator.comparing(Person::getName);
	Person maxUserByName = Collections.max(list, compByName);
	System.out.println(maxUserByName.getName()); // Output: Vivek
	
	Comparator<Person> compByAge = Comparator.comparing(Person::getAge);
	Person maxUserByAge = Collections.max(list, compByAge);
	System.out.println(maxUserByAge.getName()); // Output: Raju	
  }
}

class Person {
  private String name;
  private int age;

  public String getName() {
    return name;
  }
  public int getAge() {
    return age;
  }
  public Person(String name, int age) {
	this.name = name;
	this.age = age;
  }
} 

Reference

Java Collections
POSTED BY
ARVIND RAI
ARVIND RAI











©2022 concretepage.com | Privacy Policy | Contact Us