Share on Google+Share on Google+

Find max and min value from Arraylist

Advertisement
In this tutorial, you will learn how to find the maximum and minimum value element from the ArrayList.

Find max and min value from Arraylist

In this tutorial, you will learn how to find the maximum and minimum value element from the ArrayList. Java provides direct methods to get maximum and minimum value from any collection class i.e max() and min() respectively. Here is an example that consists of ArrayList of integers and using the max and min methods of Collections class, we have find the largest and smallest values from the list.

Example:

import java.util.*;

class FindMaxMinUsingCollection 
{
    public static void main(String[] args){

        ArrayList list = new ArrayList();
        list.add(50);
        list.add(15);
        list.add(450);
        list.add(550);
        list.add(200);
        Object minValue = Collections.min(list);
        Object maxValue = Collections.max(list);
        System.out.println("Minimum value is = " + minValue);
        System.out.println("Maximum value is = " + maxValue);

    }
}

Output:

Minimum value is = 15
Maximum value is = 550

Advertisements

Advertisement

Posted on: December 10, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.