How To Find Minimum And Maximum Values In An Arraylist In Java Collections.Min() And Collections.Max() Methods

How To Find Minimum And Maximum Values In An Arraylist In Java Collections.Min() And Collections.Max() Methods

SOURCE CODE

MaximumAndMinimumJavaCollectionMaxAndMin.java

/*
 * How To Find Minimum And Maximum Values In An Arraylist In Java Collections.Min() And Collections.Max() Methods.
 * https://mauricemuteti.info/how-to-find-minimum-and-maximum-values-in-an-arraylist-in-java-collections-min-and-collections-max-methods/
 */
package maximumandminimumjavacollectionmaxandmin;

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

/**
 *
 * @author Authentic
 */
public class MaximumAndMinimumJavaCollectionMaxAndMin {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        //Creating An ArrayList
        ArrayList<Integer> values = new ArrayList<>();
        values.add(100);
        values.add(50);
        values.add(30);
        values.add(70);
        values.add(50);
        values.add(120);
        values.add(1000);

        //Printing Aarraylist values on the console.
        for (int i = 0; i < values.size(); i++) {
            System.out.println("Value At Index (" + i + ") " + values.get(i));
        }

        int max = Collections.max(values);

        //Getting the maximum value.
        System.out.println("");
        System.out.println("The maximum value is " + max);

        int min = Collections.min(values);
        //Getting the minimum value.
        System.out.println("The minimum value is " + min);
    }

}

NETBEANS 8.2 OUTPUT

run:
Value At Index (0) 100
Value At Index (1) 50
Value At Index (2) 30
Value At Index (3) 70
Value At Index (4) 50
Value At Index (5) 120
Value At Index (6) 1000

The maximum value is 1000
The minimum value is 30
BUILD SUCCESSFUL (total time: 0 seconds)

VIDEO TUTORIAL

Leave a Reply

Your email address will not be published. Required fields are marked *