Java program to get minimum and maximum from a list



Minimum and Maximum Values of a List

A list is an ordered collection that allows us to store and access elements sequentially. It contains index-based methods to insert, update, delete, and search the elements. It can also have duplicate elements.

The following are the ways to get the minimum and maximum from a list in Java:

  • Using Loops
  • Using Collections
  • Using Streams

Minimum and Maximum of a List Using Loops

We can find the minimum and maximum values in a list by iterating through the elements using loops. We will initialize two variables to save the minimum and maximum values, and then compare each element with these variables.

Example

Below is an example of finding the minimum and maximum values in a list using loops:

Open Compiler
import java.util.ArrayList; import java.util.List; public class MinMax { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(5); numbers.add(30); numbers.add(15); int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int number : numbers) { if (number < min) { min = number; } if (number > max) { max = number; } } System.out.println("Minimum value: " + min); System.out.println("Maximum value: " + max); } }

Following is the output of the above code:

Minimum value: 5
Maximum value: 30

Minimum and Maximum of a List Using Collections

Java Collections provides utility methods to find the minimum and maximum values in a list. We can use the Collections.min() and Collections.max() methods to achieve this.

Example

Following is an example of finding the minimum and maximum values in a list using Collections:

Open Compiler
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class MinMaxCollections { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(5); numbers.add(30); numbers.add(15); int min = Collections.min(numbers); int max = Collections.max(numbers); System.out.println("Minimum value: " + min); System.out.println("Maximum value: " + max); } }

Following is the output of the above code:

Minimum value: 5
Maximum value: 30

Minimum and Maximum of a List Using Streams

Java Streams help us find the minimum and maximum values in a list using functional programming. We can use the stream() method along with min() and max() methods to achieve this.

Example

Open Compiler
import java.util.ArrayList; import java.util.List; import java.util.Optional; public class MinMaxStreams { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(5); numbers.add(30); numbers.add(15); Optional<Integer> min = numbers.stream().min(Integer::compareTo); Optional<Integer> max = numbers.stream().max(Integer::compareTo); System.out.println("Minimum value: " + min.orElse(null)); System.out.println("Maximum value: " + max.orElse(null)); } }

Following is the output of the above code:

Minimum value: 5
Maximum value: 30

Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-06-24T12:51:19+05:30

979 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements