In this article, we will understand how to get minimum and maximum from a list. A list is an ordered collection that allows us to store and access elements sequentially. It contains the index-based methods to insert, update, delete and search the elements. It can also have duplicate elements.
Below is a demonstration of the same −
Suppose our input is −
Input list: [500, 650, 300, 250, 110]
The desired output would be −
The minimum value of the list is: 110 The maximum value of the list is: 650
Algorithm
Step 1 - START Step 2 - Declare a list namely input_list. Step 3 - Define the values. Step 4 - Sort the list using the inbuilt function .sort(). Step 5 - Use the inbuilt function Integer.MAX_VALUE to fetch the max value of the list and Integer.MIN_VALUE to fetch the minimum value of the list. Step 6 - Display the result Step 7 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
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
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
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