How to Find the Minimum and Maximum Value from Java HashSet? Last Updated : 28 Dec, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report HashSet is used to store distinct values in Java. The HashSet does not guarantee the constant order of elements over time, which means when we iterate a HashSet, there is no guarantee that we get the same order of elements as we added in order. HashSet does not provide any built-in method to get the maximum and minimum values. There are a couple of ways to find the maximum and minimum from the HashSet in Java: Using Collection classUsing simple iteration Method 1: Using Collections class Using Collections class in Java we can find maximum and minimum value with the help of max() and min() method of Collections class. Code: Java // Java code to find the maximum and minimum in HashSet import java.util.*; public class GFG { public static void main(String[] args) { // New HashSet HashSet<Integer> set = new HashSet<>(); // Add data to Hashset set.add(10); set.add(20); set.add(20); set.add(10); set.add(50); set.add(40); // Print Maximum value using max method of // Collections class System.out.println("Maximum value of HashSet : " + Collections.max(set)); // Print Maximum value using max method of // Collections class System.out.println("Minimum value of HashSet : " + Collections.min(set)); } } OutputMaximum value of HashSet : 50 Minimum value of HashSet : 10 Method 2: Using Simple Iteration We can find Maximum and Minimum using simply iterate the HashSet and maintain the min and max variable and update it accordingly while traversing through each element and comparing it with the min and max values. Code: Java // Java code to find the maximum and minimum in HashSet import java.util.*; public class GFG { public static void main(String[] args) { // New HashSet HashSet<Integer> set = new HashSet<>(); // Add data to Hashset set.add(10); set.add(20); set.add(20); set.add(10); set.add(50); set.add(40); int max = -1, min = -1; // Iterate HashSet to get Maximum value for (int val : set) { if (max == -1) { max = val; } else if (val > max) { max = val; } } // Iterate HashSet to get Minimum value for (int val : set) { if (min == -1) { min = val; } else if (val < min) { min = val; } } // Print Maximum value using max method of // Collections class System.out.println("Maximum value of HashSet : " + max); // Print Maximum value using max method of // Collections class System.out.println("Minimum value of HashSet : " + min); } } OutputMaximum value of HashSet : 50 Minimum value of HashSet : 10 Comment More infoAdvertise with us Next Article How to Find the Minimum or Maximum Element from the Vector in Java? N nikhilchhipa9 Follow Improve Article Tags : Java Java Programs java-hashset Practice Tags : Java Similar Reads How to Find the Minimum or Maximum Element from the Vector in Java? The Collection is a framework offered by Java that provides an architecture to store a group of objects. One such collection is Vector(). There are many ways through which we can find the minimum and maximum elements in a Vector. These methods have been discussed below: Methods: Using Collection.min 3 min read How to Get Size, Minimum, and Maximum Value of Data Types in Java? The size of a data type is given by (name of datatype).SIZE. The maximum value that it can store is given by (Name of data type).MAX_VALUE. The minimum value that it can store is given by (Name of data type).MIN_VALUE. Always write first word of data type in capital. Example: 1. If you want to print 2 min read How to Find the Minimum or Maximum Element from LinkedHashSet in Java? Given a LinkedHashSet the task is to find the minimum and maximum element from this LinkedHashSet in Java. There are two ways to find the minimum or maximum element from LinkedHashSet in Java as follows Using the min and max method of the Collections classBy Iterating the LinkedHashSetExamples: Inpu 2 min read Finding the Minimum or Maximum Value in Java ArrayList The minimum value is the one with the smallest value and the maximum value is the one with the largest value. The main task here is to find the minimum and maximum value from the ArrayList. Consider an example of an ArrayList, and we need to find the largest and the smallest element. Example: Input 5 min read Java Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix 3 min read Finding Maximum Element of Java HashSet Java HashSet class is used to create collection to be used by the collection that uses a hash table for storage purposes that uses the mechanism known as hashing. The implementation class of Set. It inherits the abstract class and implements et interface. The main trait is it does not allow duplicat 4 min read Like