tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Collections > Collection Min and Max

Collection Min and Max 

The following example shows how to use Collections min and max API. It returns the minimum/maximum element of the given collection, according to the natural ordering of its elements.

  • Collections.min()
  • Collections.max()

File Name  :  
com/bethecoder/tutorials/utils/collections/CollectionsMinMax.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.collections;

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

public class CollectionsMinMax {

  public static void main (String [] args) {
    
    List<Integer> intList = Arrays.asList(new Integer [] {
        29846
    });
    
    //Returns the maximum/minimum element of the given collection, 
    //according to the natural ordering of its elements.
    System.out.println("Max of " + intList + " is " + Collections.max(intList));
    System.out.println("Min of " + intList + " is " + Collections.min(intList));

    List<String> strList = Arrays.asList(new String [] {
        "A""AN""THE""I""WE"
    });
    
    System.out.println("Max of " + strList + " is " + Collections.max(strList));
    System.out.println("Min of " + strList + " is " + Collections.min(strList));
  }
}
   

It gives the following output,
Max of [2, 9, 8, 4, 6, 0] is 9
Min of [2, 9, 8, 4, 6, 0] is 0
Max of [A, AN, THE, I, WE] is WE
Min of [A, AN, THE, I, WE] is A



 
  


  
bl  br