Java Collections max() Method



Description

The Java Collections max(Collection<? extends T>) method is used to return the maximum element of the given collection, according to the natural ordering of its elements.

Declaration

Following is the declaration for java.util.Collections.max() method.

public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

Parameters

coll − The collection whose maximum element is to be determined.

Return Value

The method call returns the maximum element of the given collection, according to the natural ordering of its elements.

Exception

  • ClassCastException − This is thrown if the collection contains elements that are not mutually comparable (for example, strings and integers).

  • NoSuchElementException − This is thrown if the collection is empty.

Java Collections max(ollection<? extends T>, Comparator<? super T>) Method

Description

The max(Collection<? extends T>, Comparator<? super T>) method is used to return the maximum element of the given collection, according to the order induced by the specified comparator.

Declaration

Following is the declaration for java.util.Collections.max() method.

public static <T> T max(Collection<? extends T> coll,Comparator<? super T> comp)							 

Parameters

  • coll − The collection whose maximum element is to be determined.

  • coll − The comparator with which to determine the maximum element.

Return Value

The method call returns the maximum element of the given collection, according to the specified comparator.

Exception

  • ClassCastException − This is thrown if the collection contains elements that are not mutually comparable using the specified comparator.

  • NoSuchElementException − This is thrown if the collection is empty.

Getting Max Value From a List of Integers Example

The following example shows the usage of Java Collection max(Collection) method to get maximum from a list of Integers. We've created a list with some integers. Using max(Collection) method, we're retrieved the maximum value and printed.

 
package com.tutorialspoint;

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

public class CollectionsDemo {
   public static void main(String args[]) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6));

      System.out.println("List: " + list);
      System.out.println("Max value: "+ Collections.max(list));
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

List: [1, 2, 3, 4, 5, 6]
Max value: 6

Getting Max Value From a List of Strings Example

The following example shows the usage of Java Collection max(Collection) method to get maximum from a list of Strings. We've created a list with some Strings. Using max(Collection) method, we're retrieved the maximum value and printed.

 
package com.tutorialspoint;

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

public class CollectionsDemo {
   public static void main(String args[]) {
      List<String> list = new ArrayList<>(Arrays.asList("A","B","C","D","E","F"));

      System.out.println("List: " + list);
      System.out.println("Max value: "+ Collections.max(list));
   }    
}  

Output

Let us compile and run the above program, this will produce the following result.

List: [A, B, C, D, E, F]
Max value: F

Getting Max Value From a List of Comparable Objects Example

The following example shows the usage of Java Collection max(Collection) method to get maximum from a list of Student objects. We've created a list with some Student object. Using max(Collection) method, we're retrieved the maximum value and printed. Here Student class implemented the comparable interface.

 
package com.tutorialspoint;

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

public class CollectionsDemo {
   public static void main(String args[]) {
      List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"), new Student(2, "Robert"), new Student(3, "Adam")));

      System.out.println("List: " + list);
      System.out.println("Max value: "+ Collections.max(list));
   }    
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
} 

Output

Let us compile and run the above program, this will produce the following result.

List: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Max value: [ 3, Adam ]

Getting Max Value From a List of Objects Using Comparator Example

The following example shows the usage of Java Collection max(Collection, Comparator) method to get maximum from a list of Student objects. We've created a list with some Student object. Using max(Collection, Comparator) method, we're retrieved the maximum value and printed.

 
package com.tutorialspoint;

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

public class CollectionsDemo {
   public static void main(String args[]) {
      List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"), new Student(2, "Robert"), new Student(3, "Adam")));
      RollNoComparator comparator = new RollNoComparator();

      System.out.println("List: " + list);
      System.out.println("Max value: "+ Collections.max(list,comparator));
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   public int getRollNo() {
      return rollNo;
   }

   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

class RollNoComparator implements Comparator<Student>{

   @Override
   public int compare(Student o1, Student o2) {
      return o1.getRollNo()-o2.getRollNo();
   }	
}

Output

Let us compile and run the above program, this will produce the following result.

List: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Max value: [ 3, Adam ]
java_util_collections.htm
Advertisements