Finding minimum and maximum element of a Collection in Java
Last Updated :
22 Nov, 2018
A
Collection is a group of individual objects represented as a single unit. Java provides
Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit. These are:

Finding minimum and maximum element of a Collection can be easily done using the Collections.min() and Collections.max() method. These are static method of Collections Class in Java. Hence they can be accessed directly with the help of class name as shown below.
Object ob = Collections.min(Collection<E> c);
Object ob = Collections.max(Collection<E> c);
As seen in the syntax above, the Collections.min() and Collections.max() methods take the Collection as parameter. This is the Collection of which max/min element is to be found. Hence any class implementing the Collection interface can be passed as the parameter in these functions, such as Arrays, LinkedList, ArrayList, Set, etc. And therefore the maximum and minimum element of any collection can be easily found.
Below examples demonstrate the procedure to find the minimum and maximum element of some of the collections.
Example 1: In List
Java
// Java program to find the minimum and
// maximum element in a List
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
// Get the ArrayList
List<Integer> list = new ArrayList<Integer>();
// populate the list
list.add(12);
list.add(53);
list.add(30);
list.add(8);
// printing the List
System.out.println("List: " + list);
// getting minimum value
// using min() method
int minList = Collections.min(list);
// getting maximum value
// using max() method
int maxList = Collections.max(list);
// printing the minimum value
System.out.println("Minimum value of list is: "
+ minList);
// printing the maximum value
System.out.println("Maximum value of list is: "
+ maxList);
}
}
Output:
List: [12, 53, 30, 8]
Minimum value of list is: 8
Maximum value of list is: 53
Example 2: In Set
Java
// Java program to find the minimum and
// maximum element in a Set
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
// Get the HashSet
Set<Integer> set = new HashSet<Integer>();
// fill the hashSet
set.add(3);
set.add(6);
set.add(2);
set.add(9);
// printing the Set
System.out.println("Set: " + set);
// getting minimum value
// using min() method
int minSet = Collections.min(set);
// getting maximum value
// using max() method
int maxSet = Collections.max(set);
// printing the minimum value
System.out.println("Minimum value of set is: "
+ minSet);
// printing the maximum value
System.out.println("Maximum value of set is: "
+ maxSet);
}
}
Output:
Set: [2, 3, 6, 9]
Minimum value of set is: 2
Maximum value of set is: 9
Example 3: In Arrays
Java
// Java program to find the minimum and
// maximum element in an Array
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
// Get the Array
Integer arr[] = { 2, 5, 1, 8, 34, 20, 4 };
// printing the Array
System.out.println("Array: " + Arrays.toString(arr));
// getting minimum value
// using min() method
int minArray = Collections.min(Arrays.asList(arr));
// getting maximum value
// using max() method
int maxArray
= Collections.max(Arrays.asList(arr));
// printing the minimum value
System.out.println("Minimum value of Array is: "
+ minArray);
// printing the maximum value
System.out.println("Maximum value of Array is: "
+ maxArray);
}
}
Output:
Array: [2, 5, 1, 8, 34, 20, 4]
Minimum value of Array is: 1
Maximum value of Array is: 34
What will happen if the collection is a Map?
Map is a different type of entity in the Java Collection Framework in which the elements are taken as
pairs instead of direct values. So the Map collection classes do not implement the Collection Interface. They implement the Map Interface instead. Hence the Collection.min() and Collection.max() wont work for Maps and will throw Compilation Error.
Example:
Java
// Java program to find the minimum and
// maximum element in a Map
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
try {
// Creating hashMap
Map<String, Integer> map
= new HashMap<String, Integer>();
// Putting key-value pairs in map
map.put("A", 10);
map.put("B", 15);
map.put("C", 20);
map.put("D", 25);
// Print the map
System.out.println("Map: " + map);
// getting minimum value using min()
int minMap = Collections.min(map);
// getting maximum value using max()
int maxMap = Collections.max(map);
// printing the minimum value
System.out.println("Minimum value of Map is: "
+ minMap);
// printing the maximum value
System.out.println("Maximum value of Map is: "
+ maxMap);
}
catch (NoSuchElementException e) {
System.out.println("Exception: " + e);
}
}
}
Output:
Compile Errors:
prog.java:25: error:
no suitable method found for min(Map)
int minMap = Collections.min(map);
^
method Collections.min(Collection extends T#1>)
is not applicable
How to find minimum and maximum element of a Map?
Though Map do not implement the Collection Interface, but the element type of Map, i.e. Key and Value class implement the Collection Interface individually. Therefore the Minimum and Maximum element can be found in Map based on their Key or Value.
Example 1: To find minimum and maximum elements of the Map based on the Key Values.
Java
// Java program to find the minimum and
// maximum element in a Map
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
// Creating hashMap
Map<String, Integer> map
= new HashMap<String, Integer>();
// Putting key-value pairs in map
map.put("A", 10);
map.put("B", 15);
map.put("C", 20);
map.put("D", 25);
// Print the map
System.out.println("Map: " + map);
// getting minimum value using min()
String minKey = Collections.min(map.keySet());
// getting maximum value using max()
String maxKey = Collections.max(map.keySet());
// printing the minimum value
System.out.println("Minimum Key of Map is: "
+ minKey);
System.out.println("Value corresponding to "
+ "minimum Key of Map is: "
+ map.get(minKey));
// printing the maximum value
System.out.println("Maximum Key of Map is: " + maxKey);
System.out.println("Value corresponding to "
+ "maximum Key of Map is: "
+ map.get(maxKey));
}
}
Output:
Map: {A=10, B=15, C=20, D=25}
Minimum Key of Map is: A
Value corresponding to minimum Key of Map is: 10
Maximum Key of Map is: D
Value corresponding to maximum Key of Map is: 25
Example 2: To find minimum and maximum elements of the Map based on the Value Keys.
Java
// Java program to find the minimum and
// maximum element in a Map
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
// Creating hashMap
Map<String, Integer> map
= new HashMap<String, Integer>();
// Putting key-value pairs in map
map.put("A", 10);
map.put("B", 15);
map.put("C", 20);
map.put("D", 25);
// Print the map
System.out.println("Map: " + map);
// getting minimum value using min()
int minValue = Collections.min(map.values());
// getting maximum value using max()
int maxValue = Collections.max(map.values());
// printing the minimum value
System.out.println("Minimum Value of Map is: "
+ minValue);
// printing the maximum value
System.out.println("Maximum Value of Map is: "
+ maxValue);
}
}
Output:
Map: {A=10, B=15, C=20, D=25}
Minimum Value of Map is: 10
Maximum Value of Map is: 25
Similar Reads
Find element with the maximum set bits in an array Given an array arr[]. The task is to find an element from arr[] which has the maximum count of set bits.Examples: Input: arr[] = {10, 100, 1000, 10000} Output: 1000 Binary(10) = 1010 (2 set bits) Binary(100) = 1100100 (3 set bits) Binary(1000) = 1111101000 (6 set bits) Binary(10000) = 10011100010000
5 min read
Collections max() method in Java with Examples max(Collection<? extends T> coll) The max() method of java.util.Collections class is used to return the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements
5 min read
Collections min() method in Java with Examples min(Collection<? extends T> coll) The min() method of java.util.Collections class is used to return the minimum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements
4 min read
Min and Max in a List in Java Given an unsorted list of integers, find maximum and minimum values in it. Input : list = [10, 4, 3, 2, 1, 20] Output : max = 20, min = 1 Input : list = [10, 400, 3, 2, 1, -1] Output : max = 400, min = -1Sorting This is least efficient approach but will get the work done. The idea is to sort the lis
5 min read
Maximum element in min heap Given a min heap, find the maximum element present in the heap. Examples: Input : 10 / \ 25 23 / \ / \ 45 30 50 40 Output : 50 Input : 20 / \ 40 28 Output : 40 Brute force approach: We can check all the nodes in the min-heap to get the maximum element. Note that this approach works on any binary tre
8 min read