Java Collections min() Method



Description

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

Declaration

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

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

Parameters

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

Return Value

The method call returns the minimum 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 min(ollection<? extends T>, Comparator<? super T>) Method

Description

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

Declaration

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

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

Parameters

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

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

Return Value

The method call returns the minimum 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 Min Value From a List of Integers Example

The following example shows the usage of Java Collection min(Collection) method to get minimum from a list of Integers. We've created a list with some integers. Using min(Collection) method, we're retrieved the minimum 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("Min value: "+ Collections.min(list));
   }    
}

Output

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

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

Getting Min Value From a List of Strings Example

The following example shows the usage of Java Collection min(Collection) method to get minimum from a list of Strings. We've created a list with some Strings. Using min(Collection) method, we're retrieved the minimum 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("Min value: "+ Collections.min(list));
   }    
}  

Output

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

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

Getting Min Value From a List of Comparable Objects Example

The following example shows the usage of Java Collection min(Collection) method to get minimum from a list of Student objects. We've created a list with some Student object. Using min(Collection) method, we're retrieved the minimum 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("Min value: "+ Collections.min(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 ]]
Min value: [ 1, Julie ]

Getting Min Value From a List of Objects Using Comparator Example

The following example shows the usage of Java Collection min(Collection, Comparator) method to get minimum from a list of Student objects. We've created a list with some Student object. Using min(Collection, Comparator) method, we're retrieved the minimum 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("Min value: "+ Collections.min(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 ]]
Min value: [ 1, Julie ]
java_util_collections.htm
Advertisements