In the process of maintaining records in Java, there can be an instance where it is required to retrieve the maximum or minimum value from a container. This operation can come into effect upon sorting the data or finding the average of the contained values. Also, this approach is effective for analyzing the range of values.
This blog includes various collections from which the maximum and minimum values can be retrieved.
How to Find Max/Min of a Collection in Java?
The maximum and minimum values from different collections can be retrieved via the “Collections.max()” and “Collections.min()” methods, respectively. These collections can be “List”, “Set”, “Array” as well as a “Map”.
Example 1: Find the Max/Min in a List in Java
This example fetches the maximum and minimum values from a list:
import java.util.*;
public class Minmaxlist {
public static void main(String[] args) {
List<Integer> a = new ArrayList<>(Arrays.asList(10, 20, 30, 40, 50));
System.out.println("Maximum List Value: " + Collections.max(a)); System.out.println("Minimum List Value: " + Collections.min(a));
}}
In the above snippet of code:
- First, import the “java.util.*” package to access all the features in the “java.util” package.
- Now, create a list of “Integer” types comprising the given integers.
- Lastly, apply the “Collections.max()” and “Collections.min()” methods to retrieve the maximum/highest and minimum/lowest values in the list.
Output
As analyzed, the maximum and minimum values from the list are fetched appropriately.
Example 2: Find the Max/Min in a Set in Java
In this example, the maximum and minimum values from the Collection i.e., “Set” will be returned:
import java.util.*;
public class Minmaxlist {
public static void main(String args[]){
Set<Integer> x = new HashSet<Integer>();
x.add(24);
x.add(26);
x.add(14);
x.add(8);
x.add(52);
System.out.println("Set Elements -> " + x);
int minVal = Collections.min(x);
int maxVal = Collections.max(x);
System.out.println("Minimum Set Value -> " + minVal);
System.out.println("Maximum Set Value -> "+ maxVal);
}}
According to this block of code:
- Create a HashSet of “Integer” type having the integers inserted via the “add()” method.
- After that, apply the “Collections.min()” and “Collections.max()” methods by referring to the created Set and storing the retrieved minimum and maximum set values in an “int” type variable.
Output
Example 3: Find the Max/Min in an Array in Java
This specific example retrieves the maximum and minimum values from an array instead:
import java.util.*;
public class Minmaxlist {
public static void main(String args[]) {
Integer x[] = {12, 23, 4, 32, 13};
System.out.println("Array Elements -> " + Arrays.toString(x));
int minArray = Collections.min(Arrays.asList(x));
int maxArray= Collections.max(Arrays.asList(x));
System.out.println("Minimum Array Value -> " + minArray);
System.out.println("Maximum Array Value -> " + maxArray);
}}
The above code, first, creates an integer array having the stated integers and prints the array as a string via the “toString()” method. Next, it implements the “Collections.min()” and “Collections.max()” methods to similarly find the min and max values in the array.
Output
Example 4: Find the Max/Min in a Map in Java
Map comprises the elements as pairs and the Map collection classes do not implement/apply the “Collection” Interface. However, the key and value class implement/apply the “Collection” Interface. Hence, the minimum and maximum items from the map can be retrieved based on their key or value, as follows:
import java.util.*;
public class Minmaxlist {
public static void main(String args[]) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("C", 1);
map.put("A", 31);
map.put("X", 11);
System.out.println("Map Key-Value Pairs -> " + map);
String minK = Collections.min(map.keySet());
String maxK = Collections.max(map.keySet());
System.out.println("Minimum Map Key -> " + minK);
System.out.println("Maximum Map Key -> " + maxK);
int minV = Collections.min(map.values());
int maxV = Collections.max(map.values());
System.out.println("Minimum Map Value -> " + minV);
System.out.println("Maximum Map Value -> " + maxV);
}}
In this code:
- Define a map containing the keys as “String” and values as “Integer”.
- In the next step, insert the corresponding values in the HashMap as “key-value” pairs.
- After that, utilize the combined “Collections.min()” and “Collections.max()” methods along with the “keySet()” method to return the minimum and maximum keys from the map.
- However, to retrieve the minimum and maximum values instead, associate the “values()” method with the discussed methods.
Output
From this output, it can be implied that the corresponding minimum and maximum keys and values are displayed accordingly.
Conclusion
The maximum and minimum values from the collections can be retrieved via the “Collections.max()” and “Collections.min()” methods, respectively. These collections can be “List”, “Set”, “Array” as well as a “Map”. The Map collection however contains the elements as “key-value” pairs which can be dealt with individually as well. This blog discussed the methodologies for finding the max and min values from various collections.