Java Collection Tutorial - Java Collections.min(Collection <? extends T> coll, Comparator <? super T> comp)








Syntax

Collections.min(Collection <? extends T> coll, Comparator <? super T> comp) has the following syntax.

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

Example

In the following code shows how to use Collections.min(Collection <? extends T> coll, Comparator <? super T> comp) method.

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/*from  w  w  w .ja va  2 s  .c o  m*/
public class Main {
   public static void main(String args[]) { 
      // create link list object 
      List<Integer>  list = new LinkedList<Integer> ();
    
      // populate the list  
      list.add(-8);  
      list.add(4);  
      list.add(-5);  
      list.add(2); 
      
      // comparing using natural ordering
      System.out.println("Min val: " + Collections.min(list));
      System.out.println("Min val: " + Collections.min(list,Collections.reverseOrder()));          
   }  
}

The code above generates the following result.