Home  >  Core Java

Collections.min in Java

By Arvind Rai, February 07, 2022
Find the Java doc of Collections.min method.
a. Minimum 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 min(Collection<? extends T> coll) 
It returns the minimum 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. Minimum element of the collection is found on the basis of specified Comparator instance. Pass Collection and Comparator instances.
static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) 
It returns the minimum 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

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

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

Example-2

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

public class CollectionsMin2 {
  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 minUser = Collections.min(list);
	System.out.println(minUser.name); // Output: Raju
  }
}

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 min element.
CollectionsMin3.java
package com.concretepage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionsMin3 {
  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 minUserByName = Collections.min(list, compByName);
	System.out.println(minUserByName.getName()); // Output: Raju
	
	Comparator<Person> compByAge = Comparator.comparing(Person::getAge);
	Person minUserByAge = Collections.min(list, compByAge);
	System.out.println(minUserByAge.getName()); // Output: Rohit	
  }
}

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