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
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
List<Integer> list = new ArrayList<Integer>();
list.add( 12 );
list.add( 53 );
list.add( 30 );
list.add( 8 );
System.out.println( "List: " + list);
int minList = Collections.min(list);
int maxList = Collections.max(list);
System.out.println( "Minimum value of list is: "
+ minList);
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
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
Set<Integer> set = new HashSet<Integer>();
set.add( 3 );
set.add( 6 );
set.add( 2 );
set.add( 9 );
System.out.println( "Set: " + set);
int minSet = Collections.min(set);
int maxSet = Collections.max(set);
System.out.println( "Minimum value of set is: "
+ minSet);
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
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
Integer arr[] = { 2 , 5 , 1 , 8 , 34 , 20 , 4 };
System.out.println( "Array: " + Arrays.toString(arr));
int minArray = Collections.min(Arrays.asList(arr));
int maxArray
= Collections.max(Arrays.asList(arr));
System.out.println( "Minimum value of Array is: "
+ minArray);
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:
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
try {
Map<String, Integer> map
= new HashMap<String, Integer>();
map.put( "A" , 10 );
map.put( "B" , 15 );
map.put( "C" , 20 );
map.put( "D" , 25 );
System.out.println( "Map: " + map);
int minMap = Collections.min(map);
int maxMap = Collections.max(map);
System.out.println( "Minimum value of Map is: "
+ minMap);
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.
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
Map<String, Integer> map
= new HashMap<String, Integer>();
map.put( "A" , 10 );
map.put( "B" , 15 );
map.put( "C" , 20 );
map.put( "D" , 25 );
System.out.println( "Map: " + map);
String minKey = Collections.min(map.keySet());
String maxKey = Collections.max(map.keySet());
System.out.println( "Minimum Key of Map is: "
+ minKey);
System.out.println( "Value corresponding to "
+ "minimum Key of Map is: "
+ map.get(minKey));
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.
import java.util.*;
public class GFG {
public static void main(String args[]) throws Exception
{
Map<String, Integer> map
= new HashMap<String, Integer>();
map.put( "A" , 10 );
map.put( "B" , 15 );
map.put( "C" , 20 );
map.put( "D" , 25 );
System.out.println( "Map: " + map);
int minValue = Collections.min(map.values());
int maxValue = Collections.max(map.values());
System.out.println( "Minimum Value of Map is: "
+ minValue);
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...