Java Get Index of Minimum Value in ArrayList

Tags: Collections ArrayList Collections min min index

This Java core tutorial to show you how to get the index value of the minimum value in an ArrayList by using Java Collection API.

Implement Collection Utils Class

At this step we create a new Java class named CollectionUtils and implement a new method named indexOfMinValue() which has one argument is a List of item and return index value of minimum value of values in the List.

CollectionUtils.java

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

public class CollectionUtils {

    public static int indexOfMinValue(List list) {
        if(list == null || list.size() == 0) {
            return -1;
        }

        return list.indexOf(Collections.min(list));
    }
}

How to get index of min value in ArrayList

The following example Java program to show you how to get min index of an ArrayList of double values.

MinValueExample1.java

import java.util.ArrayList;
import java.util.List;

public class MinValueExample1 {

    public static void main(String... args) {
        List list = new ArrayList();
        list.add(6.5);
        list.add(7.3);
        list.add(2.1);

        int minIndex = CollectionUtils.indexOfMinValue(list);

        System.out.println("Index of min value: " + minIndex);
    }

}
The output as below.
Index of min value: 2

In the Java program below we use the CollectionUtils class above to get min index of an ArrayList of integer values.

MinValueExample2.java

import java.util.ArrayList;
import java.util.List;

public class MinValueExample2 {
    public static void main(String... args) {
        List list = new ArrayList();
        list.add(2);
        list.add(1);
        list.add(4);

        int minIndex = CollectionUtils.indexOfMinValue(list);

        System.out.println("Index of min value: " + minIndex);
    }
}
The output as below.
Index of min value: 1

In case we have an empty List the CollectionUtils.indexOfMinValue() method return -1 as following Java example program.

MinValueExample3.java

import java.util.ArrayList;
import java.util.List;

public class MinValueExample3 {
    public static void main(String... args) {
        List list = new ArrayList();

        int minIndex = CollectionUtils.indexOfMinValue(list);

        System.out.println("Index of min value: " + minIndex);
    }
}
The output as below.
Index of min value: -1

Happy Coding 😊

How to use Java ArrayList with Examples

Java Create ArrayList using List Interface

Java Convert ArrayList to Array

Java Convert ArrayList to String

Java Convert ArrayList to Comma Separated String

Java Convert ArrayList to HashSet

Java Convert ArrayList to LinkedList

Convert Array to List in Java

Java Create New Array with Class Type and Length