Get Minimum and Maximum from a List in Java



In this article, we will understand how to get minimum and maximum from a list in Java. 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.

Problem Statement

Write a program in Java to get the minimum and maximum from a list. Below is a demonstration of the same ?

Input

Input list: [500, 650, 300, 250, 110]

Output

The minimum value of the list is: 110 The maximum value of the list is: 650

Different approaches

Following are the different approaches to getting minimum and maximum from a list ?

Using main() method

Following are the steps to get the minimum and maximum from a list using the main() method ?

  • First, we will import the ArrayList, Collections, and List from java.util package.
  • Create a new ArrayList named input_list to store the integers.
  • Add elements (e.g., 500, 650, 300, 250, 110) to input_list.
  • Print the input_list to display the initial values.
  • Copy input_list to a new list, sortedlist, and sort sortedlist using Collections.sort().
  • Check if sortedlist is empty or null:
    • If it is, print the minimum value as Integer.MAX_VALUE.
    • Print the minimum value from sortedlist (the first element).
  • We will check againif sortedlist is empty or null:
    • If it is, print the maximum value as Integer.MIN_VALUE and stop further execution.
    • Print the maximum value from sortedlist (the last element).

Example

Here, we bind all the operations together under the main() method ?

Open Compiler
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Demo { public static void main(String[] args) { System.out.println("Required packages have been imported"); List<Integer> input_list = new ArrayList<>(); input_list.add(500); input_list.add(650); input_list.add(300); input_list.add(250); input_list.add(110); System.out.println("The list is defined as " +input_list); List<Integer> sortedlist = new ArrayList<>(input_list); Collections.sort(sortedlist); if (sortedlist == null || sortedlist.size() == 0) { System.out.println("\nThe minimum value of the list is: " +Integer.MAX_VALUE); } System.out.println("\nThe minimum value of the list is: " +sortedlist.get(0)); if (sortedlist == null || sortedlist.size() == 0) { System.out.println("The maximum value of the list is: " + Integer.MIN_VALUE); return ; } int list_size = sortedlist.size() - 1; System.out.println("The maximum value of the list is: " + sortedlist.get(list_size)); } }

Output

Required packages have been imported
The list is defined as [500, 650, 300, 250, 110]

The minimum value of the list is: 110
The maximum value of the list is: 650

Using encapsulation

Following are the steps to get the minimum and maximum from a list using the encapsulation ?

  • First, we will import ArrayList, Collections, and List from java.util package.
  • Define two methods, get_min_value, and get_max_value, which take a List. Each method checks if the list is empty or null, returning default values (Integer.MAX_VALUE or Integer.MIN_VALUE) if true. Otherwise, get_min_value returns the first element of the sorted list (minimum) and get_max_value returns the last element (maximum).
  • In the main method, create an ArrayList called input_list, add elements (e.g., 500, 650, 300, 250, 110), and print it.
  • Copy input_list to sortedlist, sort it, and then use get_min_value(sortedlist) and get_max_value(sortedlist) to print the minimum and maximum values.

Example

Here, we encapsulate the operations into functions exhibiting object-oriented programming ?

Open Compiler
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Demo { public static Integer get_min_value(List<Integer> sortedlist) { if (sortedlist == null || sortedlist.size() == 0) { return Integer.MAX_VALUE; } return sortedlist.get(0); } public static Integer get_max_value(List<Integer> sortedlist) { if (sortedlist == null || sortedlist.size() == 0) { return Integer.MIN_VALUE; } int list_size = sortedlist.size() - 1; return sortedlist.get(list_size); } public static void main(String[] args) { System.out.println("Required packages have been imported"); List<Integer> input_list = new ArrayList<>(); input_list.add(500); input_list.add(650); input_list.add(300); input_list.add(250); input_list.add(110); System.out.println("The list is defined as " +input_list); List<Integer> sortedlist = new ArrayList<>(input_list); Collections.sort(sortedlist); System.out.println("\nThe minimum value of the list is: " + get_min_value(sortedlist)); System.out.println("The maximum value of the list is: " + get_max_value(sortedlist)); } }

Output

Required packages have been imported
The list is defined as [500, 650, 300, 250, 110]

The minimum value of the list is: 110
The maximum value of the list is: 650
Updated on: 2024-11-04T18:43:38+05:30

240 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements