Monday, 3 October 2016

Java Program to find Minimum & Maximum Element From ArrayList Or Collection ?

This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.

Previously we have discussed the Program for 
In Java Which run first Static Block, Block, Constructor or Method() ?


Today we learn how to find Minimum and Maximum value from Array List using predefined method available in java.util.Collections; package i.e Collections.min(Object obj); to find minimum element and Collections.max(Object obj); to find maximum element.

import java.util.ArrayList;
import java.util.Collections;

public class MinAndMax {

 public static void main(String[] args)  {
  ArrayList al=new ArrayList();
  al.add(2);
  al.add(0);
  al.add(5);
  al.add(8);
  al.add(6);
  al.add(3);
  System.out.println("given array is :" + al);
  int n=Collections.min(al);
  System.out.println("Minimum value in array is:"+ n);
  
  int m=Collections.max(al);
  System.out.println("Maximum value in array is:"+ m);
 }
}

Program Output :-

given array is :[2, 0, 5, 8, 6, 3]
Minimum value in array is:0
Maximum value in array is:8


No comments:

Post a Comment